How to get a Doctrine2 result object as an associative array? - zend-framework

I have a simple entity which is a table holding my user data
and I want to fetch all columns of a specific user as an array and then json_encode them but what I get is an entity object which I will have to use get method for every value. I just want an associative array of my user table values.
The codes I tried and didn't work (returned entity object) are as follows:
1.
$qb = $this->em->createQueryBuilder();
$qb->add('select', 'a')
->add('from', 'Entities\Adminprofile a')
->add('where', 'a.userid = 3333');
$accounts = $qb->getQuery()->getResult();
2.
$account = $this->em->getRepository('Entities\Adminprofile')->findOneBy(
array('userid' => '3333'));
PS: im using z2d2 Project,which is doctrine2 integration into Zend framework.

When you do $accounts = $qb->getQuery()->getResult(); the argument you pass to getResult tells it how to hydrate the result set which is will return.
Array Hydration
If you want arrays, than you should pass the CONSTANT for array hydrations Doctrine\ORM\Query::HYDRATE_ARRAY.
$accounts = $qb->getQuery()->getResult( Doctrine\ORM\Query::HYDRATE_ARRAY );
If you are using findOneBy() then it will always return an entity. Due to the internals of how find works, you cannot tell it to hydrate by any other means other than to return entities.
In this scenario, what you need to do is create a getValues() method inside of your entity which returns an array of your entity, like this:
public function getSimpleValues(){
return array(
'id' => $this->getId(),
'lft' => $this->getLft(),
'rgt' => $this->getRgt(),
'name' => $this->getName(),
'md5Name' => $this->getMd5Name(),
'owner' => $this->getOwner()->getId(),
'etag' => $this->getEtag()
);
}
Hydration API Docs: http://www.doctrine-project.org/api/orm/2.1/namespace-Doctrine.ORM.Internal.Hydration.html

You can also use getArrayResult() as a shortcut to passing in the constant to get an array back:
$accounts = $qb->getQuery()->getArrayResult();

You should use constant containing value 2 and it is inbuilt, you can do it like this at the end part of your query
$qb->getQuery()->getResult( Doctrine\ORM\Query::HYDRATE_ARRAY );

$data = $this->entity->findOneBy(array('key' => $value));
$hydrator = new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($this->_em, $entity_name);
$array = $hydrator->extract($data);

Related

Add a Field to a CrudController that ONLY passes values to Store / Update Methods

I'm trying to handle how a field within a CrudController stores or updates the data on the particular model in a completely custom way. I would like the traitStore() and traitUpdate() methods to ignore this field entirely, but would like the data to still be passed in via the request. This is specifically in reference to a many-many relationship using a select2_multiple field.
I would like it so that the relationship ID's are passed via the request object to the Store or Update methods, but I DO NOT want the traitStore() or traitUpdate() methods to actually perform updates on that particular field reference.
For example...
I have this field within my crud controller
$this->crud->addField(
[
'label' => "Groups",
'type' => 'select2_multiple',
'name' => 'groups',
'entity' => 'groups',
'attribute' => 'title',
'model' => "App\Models\Group",
'pivot' => true
]
);
And I'm overriding the Store and Update Methods like so.
public function store()
{
$this->crud->setValidation(UserRequest::class);
// WOULD LIKE TO SAVE EVERYTHING BUT IGNORE THE GROUPS FIELD
$response = $this->traitStore();
// DO WHATEVER I WANT WITH GROUPS AT THIS POINT
$groups = $request->groups
return $response;
}
public function update()
{
$this->crud->setValidation(UserRequest::class);
// WOULD LIKE TO SAVE EVERYTHING BUT IGNORE THE GROUPS FIELD
$response = $this->traitUpdate();
// DO WHATEVER I WANT WITH GROUPS AT THIS POINT
$groups = $request->groups
return $response;
}
Looking at my comments I would like to get a reference to the groups and handle updating the model however I want.
I've tried to unset the groups value in the request, unset($this->request{'groups'}), but it still updates / removes the relationships when I do that.
Here is what you need to do to remove the references from being updated by the CrudController.
public function update()
{
$this->crud->setValidation(UserRequest::class);
$request = clone $this->request;
$this->crud->request->request->remove('groups');
$this->crud->removeField('groups');
$groups = $request->groups
$response = $this->traitUpdate();
return $response;
}
I found an easy way to ignore/pass form field.
Example:
In your form fields have first_name, last_name, gender and in your database is only have fullname, gender then you wanna create/update the form, it will show Column not found: 'first_name' not found...,
How to fix it:
Add $fillable in the model and fill the array data with name field that you want to store/update. In example case $fillable = ['fullname', 'gender'];
Then, just use mutators inside the model too.
public function setFullnameAttribute(){
return $this->attributes['fullname'] = \Request::input('first_name') . ' ' . \Request::input('last_name');
}
NB: You should have hidden field name 'fullname' in your CrudController.
$this->crud->addField(['name' => 'fullname', 'type' => 'hidden']);

How to get data through ModelTransformer

Here is some code of my Type Class. For one of the property I use a ModelTransform to display what I want :
$builder->add('myProperty', 'text', array(
'label' => 'MyLabel',
'required' => true,
'disabled' => true
));
$builder->get('myProperty')
->addModelTransformer(new CallbackTransformer(
function($propertyFrom) {
return (round($propertyFrom/ 100, 3)) + 1;
},
function($propertyTo) {
return round((($propertyTo- 1) * 100), 3);
}
));
it works good when I display my form in the twig.
I would like to get the entity from my form. I guessed, assuming I have my Form object, I would do like this :
$entity = $form->getViewData()
I wanted to have the entity with the value of the fields passed through the ModelTransform. Though I have the value of field with no transformation.
Finally I wonder what is the difference between $form->getData() and $form->getViewData().
How can I get the entity with the values transformed by my ModelTransformer ?
It is a Symfony bug that does not apply transformer via the getViewData on the object. But it does it on the field.
getViewData vs. getNormData and DataTransformers - different results depending on context
This is a kind of limitation because compound forms having an object or array as underlying data, don't keep their view data synchronized with their children's view data. https://github.com/symfony/symfony/issues/18683#issuecomment-249676768
You can get the difference thus:
// if myProperty value is equal to 23
$form->get('myProperty')->getData(); //output: 23
$form->get('myProperty')->getNormData(); //output: 1.23
$form->get('myProperty')->getViewData(); //output: "1.23"

How should a `FormType` be structured in order to include an `EntityType` field which doesn't exist in the `FormType` entity?

Taking the below as an example, OrderType is based on the entity Order. The form that is required needs to contain the following two EntityType dropdowns within it:
Category (this does not exist in Order - it is just to subset the dropdown of Product to make it more manageable)
Product
The identifying Category variables (Category_id and CatName) only exists within the Product entity (the Order can include multiple Products) and as a result, Symfony throws back an error saying:
Neither the property "category_id" nor one of the methods "getcategory_id()", "category_id()", "iscategory_id()", "hascategory_id()", "__get()" exist and have public access in class "AppBundle\Entity\Order".
Is there a way that this Category field can be included even though it doesn't exist in the Order entity?
It doesn't seem right to add category_id to the Order entity. I was thinking of something along the lines of below using 'mapped'=>'false' but I can't get it to work:
$builder
->add('category_id','entity',array(
'class'=>'AppBundle:Product',
'placeholder' => '-- Choose --',
'choice_label'=>'CatName',
'mapped'=>'false',
'query_builder'=>function(EntityRepository $er) {
return $er->createQueryBuilder('p');
}))
...and then after an Ajax response, feed in the category back in with $category?
->add('products','entity',array(
'class'=>'AppBundle:Order',
'placeholder' => '-- Choose --',
'choice_label'=>'ProductName',
'query_builder'=>function(EntityRepository $er, $category ) {
return $er->createQueryBuilder('p')
->where('p.category_id = :id')
->setParameter('id', $category )
->orderBy('p.ProductName','ASC');
}));
}
As you say, adding a Category property to the Order entity just for forms is less than ideal. What I would do is make a OrderCategoryType and pass in Order and Category as an array.
// Controller
$order = new Order();
$category = new Category(); // Or create from $order->getProduct()
$data = ['order' => $order, 'category' => $category);
$form = $this->createFormBuilder($data)
->add('order',new OrderType(),
->add('category',new CategoryType()
);
You will have to do some messing around to keep everything in sync but it should work just fine.

Entity mapping in a Symfony2 choice field with optgroup

Suppose to have an entity in Symfony2 that has a field bestfriend, which is a User entity selected from a list of User entities that satisfy a complex requirement.
You can render this field in a form by specifying that it is an entity field type, i.e.:
$builder->add('bestfriend', 'entity', array(
'class' => 'AcmeHelloBundle:User',
'property' => 'username',
));
This form field is rendered as a <select>, where each one of the displayed values is in the form:
<option value="user_id">user_username</option>
So, one would render the field by using the <optgroup> tags to highlight such special feature of the friends.
Following this principle, I created a field type, namely FriendType, that creates the array of choices as in this answer, which is rendered as follows:
$builder->add('bestfriend', new FriendType(...));
The FriendType class creates a <select> organized with the same <option>s but organized under <optgroup>s.
Here I come to the problem! When submitting the form, the framework recognize that the user field is not an instance of User, but it is an integer. How can I let Symfony2 understand that the passed int is the id of an entity of type User?
Here follows my solution.
Notice that it is not mentioned in the Symfony2 official docs, but it works! I exploited the fact that the entity field type is child of choice.
Hence, you can just pass the array of choices as a param.
$builder->add('bestfriend', 'entity', array(
'class' => 'AcmeHelloBundle:User',
'choices' => $this->getArrayOfEntities()
));
where the function getArrayOfEntities() is a function that fills the choice list with the friends of my friends, organized by my friends:
private function getArrayOfEntities(){
$repo = $this->em->getRepository('AcmeHelloBundle:User');
$friends = $repo->findAllFriendByComplexCriteria(...);
$list = array();
foreach($friends as $friend){
$name = $friend->getUsername();
if(count($friend->getFriends())>0){
$list[$name] = array();
foreach($friend->getFriends() as $ff){
$list[$name][$ff->getUsername()] = $ff;
}
}
}
return $list;
}
I know the example could be meaningless, but it works...
PS: You need to pass the entity manager to let it working...

Doctrine 2 + Zend Form - Populate Dynamic Select Menus

I'm building a Zend form that has a dropdown/select menu populated with data from a Doctrine 2 query.
In my repository class, I have the following query in a method named selectUser():
$query = $em->createQuery('SELECT u.id, u.name FROM XX\Entity\Users u ORDER BY u.name ASC');
$users = $query->getResult();
This returns a multidimensional array, which I'm trying to loop through like this (within the same method):
$options = array();
foreach ($users as $key => $value) {
$options[$value['id']] = $value['name'];
}
return $options;
Then in my Zend form class, I try to populate the Select element like this:
$id = new Zend_Form_Element_Select('id');
$options = $this->usersRepository->selectUser();
$id->AddMultiOptions($options);
The result is an error for each user row that states "Undefined index: [name] in ...UsersRepository.php..." where [name] is the value of the 'name' column in each row.
Does anyone see what I'm doing wrong or how to populate a dynamic select menu using Doctrine 2 and Zend Framework?
(By the way, in order to run the repository method, the form class has protected properties representing the Doctrine container, entity manager, and Users repository. If this isn't considered best practice, I'd welcome any suggestions on improving my technique.)
I think your problem is here
$options[$value['id'] = $value['name']];
this would be better
$options[$value['id']] = $value['name'];