ZFcuser get information with inner join - zend-framework2

I have a user table with a store id column (store) that correspondent with a store table.
I'm retrieving this store id with a custom entity object.
Application/Entity/User.php
namespace Thuiswinkelen\Entity;
class User extends \ZfcUser\Entity\User
{
/**
* #var int
*/
protected $store;
public function getStore()
{
return $this->store;
}
/**
* Set store.
*
* #param int $store
* #return UserInterface
*/
public function setStore($store)
{
$this->store = (int) $store;
return $this;
}
}
My question is: How to get the store name in the store table (with an inner join?)
It would be great when I can use something like:
<?php echo $this->zfcUserStoreId() ?>
<?php echo $this->zfcUserStoreName() ?>

If you're looking to map entity relationships; you will need an object relational mapper (ORM), such as Doctrine to accomplish this.
This will convert your foreign identifiers into objects in which you can then transverse.
$storeName = $user->getStore()->getName();
I'm guessing that the examples you have suggested $this->zfcUserStoreId() you have invented due to the already existing ZfcUser\View\Helper\ZfcUserDisplayName
This however is a view helper and simplify aids the rendering of a user's name based on other configuration.

Related

zend-authentication - setting identity to custom object with rbac roles loaded

In a ZF2 project i am using the AuthenticationService to validate a users log in credentials. This is working fine, except it only stores in the session a string containing the users name.
What i would like would be for subsequent calls to AuthenticationService::getIdentity to return a custom Identity object, that is populated with the users database id, roles and permissions (popualted from an RBAC service), so that the object in the session is a bit more useful.
I am able to create this object, but am unsure of the best way to keep it in the session; ideally i would like to override the entry with the key Zend_Auth, but this does not seem to be working.
My code so far:
<?php
namespace Authentication\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Authentication\AuthenticationService;
use Authentication\Form\Login\LoginForm;
use Zend\Form\Form;
use Authentication\Model\Identity\AuthenticatedIdentity;
class AuthenticationController extends AbstractActionController
{
/**
*
* #var AuthenticationService
*/
protected $authenticationService;
/**
*
* #var LoginForm
*/
protected $loginForm;
/**
*
* #param AuthenticationService $authenticationService
* #param LoginForm $loginForm
*/
public function __construct(AuthenticationService $authenticationService, LoginForm $loginForm){
$this->authenticationService = $authenticationService;
$this->loginForm = $loginForm;
}
public function indexAction(){
$form = $this->loginForm;
$viewModel = new ViewModel();
$viewModel->setVariables([
'loginForm' => $form
]);
if($this->getRequest()->isPost() === false){
return $viewModel;
}
$form->setData($this->getRequest()->getPost());
if($form->isValid() === false){
return $viewModel;
}
$data = $form->getData();
$authenticationAdapter = $this->authenticationService->getAdapter();
$authenticationAdapter->setIdentity($data['credentials']['username'])
->setCredential($data['credentials']['password']);
$authenticationResult = $this->authenticationService->authenticate($authenticationAdapter);
if($authenticationResult->isValid() === false){
$viewModel->setVariable('validCredentials', false);
return $viewModel;
}
/**
* Create a user model and save it to the session.
*/
$authenticationResultRow = $authenticationAdapter->getResultRowObject(null, ['password']);
$permissions = $this->rbacService->getPermissionsForUser($authenticationResultRow->user_id);
$roles = $this->rbacService->getRolesForUser($authenticationResultRow->user_id);
$identity = new AuthenticatedIdentity(
$authenticationResult->getIdentity(),
'admin',
$permissions,
$roles
);
$identity->setUserId($authenticationResultRow->user_id);
//how to store this Identity object in session so AuthenticationService will return it?
return $this->redirect()->toRoute('dashboard');
}
}
Check out https://github.com/zendframework/zend-authentication/blob/master/src/AuthenticationService.php#L75 and https://github.com/zendframework/zend-authentication/blob/master/src/Storage/StorageInterface.php
You can write the AuthenticatedIdentity object directly to the storage like so:
$this->authenticationService->getStorage()->write($identity);
However, I would advice against doing so because:
If the user's permissions/roles change during the session he/she would have to log out and back in to see any changes which is not very user-friendly.
Your AuthenticatedIdentity object and all objects it contains need to be serializable, which can become problematic to maintain.
I would (and do) fetch the user object and/or roles when needed, either from DB or some form of cache but don't store it in the session.

How to getData() from a Zend\Form before the validation in Zend Framework 2/3?

I have a complex nested (order) Zend\Form, that can be edited multiple times. The user first creates an order, but doesn't need to place it immediately. He can just save the order (or more exact: its data) and edit it later. In this case the application loads an Order object (with all its nested structure) and binds it to the form. The important steps are:
get ID of the order from the request
get the Order object by ID
$orderForm->bind($orderObject)
...
Now I want to catch the data and serialize it to JSON. (The background: Forms cloning -- in the next step a empty new form should created and the should be passed to it; after saving we'll get a clone.) It should happen between 2 and 3. So I'm trying
$formData = $this->orderForm->getData();
$formJson = json_encode($formData, JSON_UNESCAPED_SLASHES);
and getting the error:
Zend\Form\Form::getData cannot return data as validation has not yet occurred
Well, I could try to work around it and validate the form:
$formIsValid = $this->orderForm->isValid();
but it only leads to further troubles:
Zend\InputFilter\BaseInputFilter::setData expects an array or Traversable argument; received NULL
Is there a way to get the form data before the validation?
Okay, the comment space is way too small to say everything about what you try to archive. Let 's refactor every single step you mentioned in the starting post. This will lead us to your goal. It 's all about hydration.
This will be a small example, how an order entity with products in it could look like. After the order entity follows the product entity, which we need for this example.
namespace Application\Entity;
class Order implements \JsonSerializable
{
/**
* ID of the order
* #var integer
*/
protected $orderID;
/**
* Array of \Application\Entity\Product
* #var array
*/
protected $products;
public function getOrderID() : integer
{
return $this->orderID;
}
public function setOrderID(integer $orderID) : Order
{
$this->orderID = $orderID;
return $this;
}
public function getProducts()
{
if ($this->products == null) {
$this->products = [];
}
return $this->products;
}
public function setProducts(array $products) : Order
{
$this->products = $products;
return $this;
}
/**
* #see \JsonSerializable::jsonSerialize()
*/
public function jsonSerialize()
{
return get_object_vars($this);
}
}
The following entity represents a product.
class Product implements \JsonSerializable
{
protected $productID;
protected $name;
public function getProductID() : integer
{
return $this->productID;
}
public function setProductID(integer $productID) : Product
{
$this->productID = $productID;
return $this;
}
public function getName() : string
{
return $this->name;
}
public function setName(string $name) : Product
{
$this->name = $name;
return $this;
}
/**
* #see \JsonSerializable::jsonSerialize()
*/
public function jsonSerialize()
{
return get_object_vars($this);
}
}
Above you see our entity, wich represents a single order with several possible products in it. The second member products can be an array with Product entities. This entity represents the data structure of our simple order.
At this point we need a form, which uses this entites as objects for the data it contains. A possible factory for our form could look like this.
namespace Application\Form\Factory;
class OrderFormFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$parentLocator = $serviceLocator->getServiceLocator();
$inputFilter = $parentLocator->get('InputFilterManager')->get(OrderInputFiler::class);
$hydrator = new ClassMethods(false);
$entity = new OrderEntity();
return (new OrderForm())
->setInputFilter($inputFilter)
->setHydrator($hydrator)
->setObject($entity);
}
}
This is the factory for our form. We set a hydrator, an input filter and an entity for the form. So you don 't have to bind something. The following code shows, how to handle data with this form.
// retrieve an order from database by id
// This returns a order entity as result
$order = $this->getServiceLocator()->get(OrderTableGateway::class)->fetchById($id);
// Extract the order data from object to array assumed the
// retrieved data from data base is an OrderEntity object
// the hydrator will use the get* methods of the entity and returns an array
$data = (new ClassMethods(false))->extract($order);
// fill the form with the extracted data
$form = $this->getServiceLocator()->get('FormElementManager')->get(OrderForm::class);
$form->setData($data);
if ($form->isValid()) {
// returns a validated order entity
$order = $form->getData();
}
It is absolutely not possible to get data from a form, that is not validated yet. You have to validate the form data and after that you can get the filtered / validated data from the form. Hydrators and entities will help you a lot when you have to handle a lot of data.

Zend\Db Model with Child Models

ZF2 project - no Doctrine, using native Zend\Db: Have the following structure:
Controller
ProductController
Model
Product
ProductTable
ProductType
ProductTypeTable
Product is the model, has variables corresponding to the “products" table fields.
ProductTable is table class which is connected to the database via tableGateway. ProductTable has getItem() method to retrieve requested product by “id”.
ProductType is the model, has variables like id, name, description corresponding to the “productTypes" table fields.
ProductTypeTable is table class just like ProductTable.
Each product belongs to a certain ProductType
products.productTypeId = productTypes.id
is the relation.
In ProductTable->getItem() method, I can simply get productTypeId.
I can use joins to get productTypes.name, productTypes.description, or any field from "productTypes" table.
But I don’t want to do this - instead dealing with new variables in Product entity like productTypeName, productTypeDesc,
I’d like to have Product->getProductType() and set it to be a ProductType object, so I can get Product->getProductType() ->getName() to get product type name.
Simply I’d like to assign a child model as a variable of the parent model.
I can do this in the controller like below:
$product = $this->getProductTable()->getItem(7); // id = 7
$product->setProductType($this->getProductTypeTable()
->getItem($product->getProductTypeId());
But I’d like to make it happen in product table class getItem() method. So I don’t have to think about it in every controller, and it is kind of encapsulated.
What is the right way to do this?
Thank you.
The issue that you have is the Table Gateway pattern is only really any good at abstracting database access to a a single database table. It does not in anyway allow for the hydration of entities or management of relationships. Object Relationship Mappers (ORM's), such as Doctrine, solve this problem.
If Doctrine, for whatever reason, is inappropriate for your use case an alternative could be implementing the Data Mapper Pattern
The Data Mapper is a layer of software that separates the in-memory objects from the database. Its responsibility is to transfer data between the two and also to isolate them from each other
The data mapper will use the table gateway to fetch the required data for each table and construct the Product instance, including it's associated ProductType. You would then expose the mapper to the controller (rather than the table gateway).
A simple example of a ProductMapper.
class ProductMapper
{
// #var \Zend\Db\TableGateway\TableGateway
protected $productTable;
protected $productTypeMapper;
// an 'identity map' of loaded products
protected $loaded = [];
public function __construct(ProductTable $productTable, ProductTypeMapper $productTypeMapper)
{
$this->productTable = $productTable;
$this->productTypeMapper = $productTypeMapper;
}
protected function hydrate(Product $product, array $data)
{
$product->setId($data['id']);
$product->setName($data['name']);
$product->setFoo($data['foo']);
if (isset($data['type_id'])) {
// Load a fully constructed product type from the database
$type = $this->productTypeMapper->findById($data['type_id']);
$product->setType($type);
}
return $product;
}
public function findById($id)
{
if (isset($this->loaded[$id])) {
return $this->loaded[$id];
}
// Get the data
$row = $this->productTable->select(['id' => $id]);
if (empty($row)) {
throw new SomeCustomException("No product could be found with id $id");
}
// Create and hydrate the product
$product = $this->hydrate(new Product, $row->current())
$this->loaded[$id] = $product;
return $product;
}
public function save(array $data);
public function update($data);
public function delete($id);
}
You can achieve this, you just have to follow the following 3 steps:
Make your Product->exchangeArray() function smarter
Get all required ProductType fields, using a prefix helps for example: type_
Add #var ProductType so you will have proper autocompete (works for me in Eclipse)
<?php
namespace Product\Model\Product;
class Product {
public $id;
...
/**
* #var ProductType
*/
public $productType;
...
public function exchangeArray( $data ) {
$this->id = (isset($data['id'])) ? $data['id'] : null;
...
$productType = new ProductType();
$typeData = array(
'id' => $data['type_id'],
'value' => $data['type_value']
);
$productType->exchangeArray( $typeData );
$this->productType = $productType;
}
}

Primefaces Datatable: Checkbox select doesn't assign selected values

I'm using primefaces 3.5, with a glassfish server 3.1.2. I have a trivia question game that relies on the user to select answers. I have two tables which one is generated based on if it's a multi-select question, or if its mutlipile choice. While my multiple choice data table works beautifully, the other does not. I've followed the example on the show case, and when I select 2 of the table, and hit the next button on the wizard it is in, it deselects what I selected and keeps me on the same page. I made it stay on the same page on any exception, and the exception was a null pointer due to the fact that the "selected answers" where null. Here is my table.
<p:dataTable
id="multiQuestionTable"
value="#{triviaQuestionsBean.dataModel}"
var="answer"
selection="#{triviaQuestionsBean.selectAnswers}">
<p:column selectionMode="multiple" />
<p:column>
#{answer.answer.testAnswer}
</p:column>
</p:dataTable>
The setting and getter:
private QuestionAnswers[] selectAnswers;
public QuestionAnswers[] getSelectAnswers() {
return selectAnswers;
}
public void setSelectAnswers(QuestionAnswers[] selectAnswers) {
this.selectAnswers = selectAnswers;
}
The setter is never called, but the data model that is used works very well for the single select. If that is needed to figure out my issue let me know. Please assist if possible.
public class QuestionAnswersDataModel extends ListDataModel
implements SelectableDataModel {
/**
* This is the question answers data model used to allow for the sorting,
* and selection of items in a JSF dataTable. This is the basic no-arg
* constructor --Important-- This judges the data from the id, so if the ID
* has not been assigned, there will be unpredictable results.
*
*/
public QuestionAnswersDataModel() {
}
/**
* This is the question answers data model used to allow for the sorting,
* and selection of items in a JSF dataTable. This is the constructor where
* the list of elements are instantiated. --Important-- This judges the data
* from the id, so if the ID has not been assigned, there will be
* unpredictable results.
*
* #param data The list of QuestionAnswers to display in the table.
*/
public QuestionAnswersDataModel(List<QuestionAnswers> data) {
super(data);
}
/**
* This takes a "row key" and looks through the wrapped data to find the
* specific QuestionAnswers entity that matches the passed in row key
*
* #param rowKey The key to search with
* #return The QuestionAnswers entity that matches the criteria or null if
* nothing matches
*/
#Override
public QuestionAnswers getRowData(String rowKey) {
/**
* Get the wrapped data (If there was a lot of data you would use a
* query not just a list)
*/
List<QuestionAnswers> answers =
(List<QuestionAnswers>) getWrappedData();
//for each answer
for (QuestionAnswers answer : answers) {
//if the answer's unique identifier matches the row key:
if (answer.getQuestionAnswersId().toString().equals(rowKey)) {
//return it
return answer;
}
}
//if nothing matches return null
return null;
}
/**
* This takes a QuestionAnswers entity object and returns a key for the
* identification of this entity. As this one runs off of the ID of the
* answer, if nothing is assigned to the value, a null key will be returned.
*
* #param answer The answer to generate the key of
* #return The identifier for this object or null if the ID is null
*/
#Override
public Object getRowKey(QuestionAnswers answer) {
//if the answer is null, return null
if (answer == null) {
return null;
}
//else get the answer id
Long id = answer.getQuestionAnswersId();
//if it's null return null
if (id == null) {
return id;
}
//else return the String representation of the id
return id.toString();
}

Doctrine DQL that produces the output of a RIGHT JOIN?

I am trying to return a list of all members, and also include their membership number if they have it.
The models are setup as listed below:
The member class:
class Member {
/**
* #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
protected $id;
/** #Column(type="string", length=255) */
protected $email = '';
}
The membership class:
class Membership {
/** #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
protected $id;
/** #OneToOne(targetEntity="Member") */
protected $member = 0;
/** #Column(type="integer") */
protected $membership_number = 0;
}
I want to create a query that returns ALL the members, AND also shows a membership number if it exists for that member.
Can't figure out how to do this.
The query below selects all members with a membership, not what I need though.
Right joins don't work so I'm not sure what to do.
$this->_em->createQuery("SELECT m, mb FROM Membership mb JOIN mb.member m");

Resources