Zf2 list elements from entities doctrine 2 - doctrine-orm

I have simple question,
how can I create form list elements, something like grid or this:
[x] name | image | [button]
[ ] name | image | [button]
[x] name | image | [button]
<table>
<tr><th>checkbox</th><th>name</th><th>action</th></tr>
<tr><td><input type="checkbox"></td><td>name</td><td><button>OK</td></tr>
<tr><td><input type="checkbox"></td><td>name</td><td><button>OK</td></tr>
<tr><td><input type="checkbox"></td><td>name</td><td><button>OK</td></tr>
</table>
//list entities from db, array(object,object,object)
//object = Application\Entity\Area
$areas = $this->getObjectManager()->getRepository('Application\Entity\Area')->findAll();
I used in form Zend\Form\Element\Collection but I don't know how populate collection date from db, so I had clear form.
I should do it properly and what to use?

From Doctrine you already get an iterable datatype (array). So you only need to iterate it in your view:
...
<?php foreach($this->data as $area): ?>
//your table row markup for a single entity
<?php endforeach; ?>
...

Disclaimer: I have asked a similar question, with no answer. So I would also be keen to know the 'Zend' way or if anyone is able to suggest an alternative.
The approach below seems to work for me.
ListForm.php
Add a collection to your 'list' form.
/** The collection that holds each element **/
$name = $this->getCollectionName();
$collectionElement = new \Zend\Form\Element\Collection($name);
$collectionElement->setOptions(array(
'count' => 0,
'should_create_template' => false,
'allow_add' => true
));
$this->add($collectionElement);
This collection will hold out collection element (Zend\Form\Element\Checkbox)
/** The element that should be added for each item **/
$targetElement = new \Zend\Form\Element\Checkbox('id');
$targetElement->setOptions(array(
'use_hidden_element' => false,
'checked_value' => 1,
));
$collectionElement->setTargetElement($targetElement);
Then I add a few methods to allow me to pass an ArrayCollecion to the form. For each entity in my collection I will create a new $targetElement; setting its it's checked value to the id of the entity.
/**
* addItems
*
* Add multiple items to the collection
*
* #param Doctrine\Common\Collections\Collection $items Items to add to the
* collection
*/
public function addItems(Collection $items)
{
foreach($items as $item) {
$this->addItem($item);
}
return $this;
}
/**
* addItem
*
* Add a sigle collection item
*
* #param EntityInterface $entity The entity to add to the
* element collection
*/
public function addItem(EntityInterface $item)
{
$element = $this->createNewItem($item->getId());
$this->get($this->getCollectionName())->add($element);
}
/**
* createNewItem
*
* Create a new collection item
*
* #param EntityInterface $entity The entity to create
* #return \Zend\Form\ElementInterface
*/
protected function createNewItem($id, array $options = array())
{
$element = clone $this->targetElement;
$element->setOptions(array_merge($element->getOptions(), $options));
$element->setCheckedValue($id);
return $element;
}
All that is then needed is to pass the collection to the form from within the controller action.
SomeController
public function listAction()
{
//....
$users = $objectManager->getRepository('user')->findBy(array('foo' => 'bar'));
$form = $this->getServiceLocator()->get('my_list_form');
$form->addItems($users);
//...
}

You can populate multi-select checkbox using doctrine from the database using DoctrineModule\Form\Element\ObjectMultiCheckbox as in this page:
https://github.com/doctrine/DoctrineModule/blob/master/docs/form-element.md
simply you need to pass the entity manager to the form, and then do same as in the example you can create ObjectMultiCheckbox form element...
or the other better -moro automated work- method, if you want to use the collection you need to do the mapping right (#orm\OneToMany and #orm\ManyToOne) with the area... and the create a fieldset in the form as in here...:
http://framework.zend.com/manual/2.2/en/modules/zend.form.collections.html
and add methods to the other entity to add and remove the areas as this:
public function addArea(Collection $areas)
{
foreach ($areas as $area) {
$area->setOtherEntity($this);
$this->areas->add($area);
}
}
public function removeAreas(Collection $areas)
{
foreach ($areas as $area) {
$area->setOtherEntity(null);
$this->areas->removeElement($area);
}
}
By this if you use the hydration the values will be added and removed as you select them automatically...

Related

Search in parent relationship with the database driver

Let's say you have this relationship: users x cats. Each user can have many cats (a "one-to-many" relationship):
class Cat extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
Both models (users and cats) have a name field.
Let's say we want to get all cats with bob in their names, using Laravel's Scout.
The standard solution is to add this to the Cat.php model:
// Cat.php
use Searchable;
/**
* Get the indexable data array for the model.
*
* #return array
*/
public function toSearchableArray()
{
return [
'name' => $this->name,
];
}
And we search with Cat::search('bob')->get().
The problem
The above solution works well, but what if we want to search in the relationship's fields?
What if you want to get cats owned by people with bob in their names?
If you add this to the "Cat" model:
// Cat.php
use Searchable;
/**
* Get the indexable data array for the model.
*
* #return array
*/
public function toSearchableArray()
{
return [
'name' => $this->name,
'users.name' => '', // no need to return `$this->user->name` as the database engine only uses the array keys
];
}
It won't work. You will get this exception when running Cat::search('bob')->get():
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.name' in 'where clause'
SQL: select `cats`.* from `cats` where (`cats`.`name` like %bob% or `users`.`name` like %bob%)
Clearly, the SQL is missing the users table. But how to add it? Doing a Cat::join(...)->search('bob') will throw an exception, same for Cat::search(...)->join(...).
The question is: How to search in the parent attributes? And by "parent" I mean the "belongsTo" model.
The query method allows for modifing the search query. Use it to inject a join clause:
Cat::search('bob')->query(function ($builder) {
$builder->select('cats.*')->join('users', 'cats.user_id', '=', 'users.id');
})->get();
This generates the proper query:
SELECT `cats`.*
FROM `cats`
INNER JOIN `users` on `cats`.`genre_id` = `users`.`id`
WHERE (`cats`.`name` LIKE '%bob%' or `users`.`name` LIKE '%bob%')
ORDER BY `id` desc
EDIT: Automatically adds the JOIN clause to all searches:
If you want to search with just Cat::search('bob')->get(), without having to write ->join(...) on every call:
// Cat.php
/**
* Overrides the "search" method to inject a `join` to the relationships.
*/
use Searchable {
Searchable::search as parentSearch;
}
/**
* Perform a search against the model's indexed data.
*
* #param string $query
* #param \Closure $callback
* #return \Laravel\Scout\Builder
*/
public static function search($query = '', $callback = null)
{
return static::parentSearch($query, $callback)->query(function ($builder) {
$builder->select('cats.*')->join('users', 'cats.user_id', '=', 'users.id');
});
}

How to select rows which have both items in ManyToMany relation

Let's assume i have "News" entity which has got ManyToMany "Tag" relation
class News
{
/**
* #ORM\ManyToMany(targetEntity="App\Domain\Entity\Vocabulary\Tag")
*/
private Collection $tags;
}
And i have such query:
public function getList(
array $tags = null,
): Query {
if (null !== $tags) {
$qb->andWhere('nt.id IN (:tags)');
$qb->setParameter('tags', $tags);
}
}
The problem is when i pass ["Tag1", "Tag2"] it selects news that have either the first tag or the second, but not both at the same time. How can i rewrite the query to select news which have both tags at the same time?
Some things to notice first:
For doctrine annotations it is possible to use the ::class-constant:
use App\Domain\Entity\Vocabulary\Tag;
class News
{
/**
* #ORM\ManyToMany(targetEntity=Tag::class)
*/
private Collection $tags;
}
If the $tags array is empty doctrine will throw an exception because an empty value set is invalid SQL, at least in mysql:
nt.id IN () # invalid!
Now to the problem:
With the SQL-aggregation functions COUNT and GROUP BY we can count the number of tags for all news. Together with your condition for the allowed tags, the number of tags per news must be equal to the number of tags in the tags array:
/**
* #var EntityManagerInterface
*/
private $manager;
...
/**
* #param list<Tag> $tags - Optional tag filter // "list" is a vimeo psalm annotation.
*
* #return list<News>
*/
public function getNews(array $tags = []): array
{
$qb = $this->manager
->createQueryBuilder()
->from(News::class, 'news')
->select('news')
;
if(!empty($tags)) {
$tagIds = array_unique(
array_map(static function(Tag $tag): int {
return $tag->getId();
}) // For performance reasons, give doctrine ids instead of objects.
); // Make sure duplicate tags are handled.
$qb
->join('news.tags', 'tag')
->where('tag IN (:tags)')
->setParameter('tags', $tagIds)
->addSelect('COUNT(tag) AS HIDDEN numberOfTags')
->groupBy('news')
->having('numberOfTags = :numberOfTags')
->setParameter('numberOfTags', count($tags))
;
}
return $qb
->getQuery()
->getResult()
;
}

What is the best practice for repository?

In my repositories, I have methods with too many arguments (for use in where) :
Example :
class ProchaineOperationRepository extends EntityRepository
{
public function getProchaineOperation(
$id = null, // Search by ID
\DateTime $dateMax = null, // Search by DateMax
\DateTime $dateMin = null, // Search by DateMin
$title = null // Search by title
)
In my controllers, I have differents action ... for get with ID, for get with ID and DateMin, for get ID and Title, ...
My method is too illegible because too many arguments ... and it would be difficult to create many methods because they are almost identical ...
What is the best practice ?
You have two main concerns in your question
You have too many arguments in your repository method which will be used in 'where' condition of the eventual query. You want to organize them in a better way
The repository method should be callable from the controller in a meaningful way because of possible complexity of arguments passed
I suggest you to write a Repository method like:
namespace AcmeBundle\Repository;
/**
* ProchaineOperationRepository
*
*/
class ProchaineOperationRepository extends \Doctrine\ORM\EntityRepository
{
public function search($filters, $sortBy = "id", $orderBy = "DESC")
{
$qb = $this->createQueryBuilder("po");
foreach ($filters as $key => $value){
$qb->andWhere("po.$key='$value'");
}
$qb->addOrderBy("po.$sortBy", $orderBy);
return $qb->getQuery()->getArrayResult();
}
}
The $filters variable here is an array which is supposed to hold the filters you are going to use in 'where' condition. $sortBy and $orderBy should also be useful to get the result in properly sequenced way
Now, you can call the repository method from your controller like:
class ProchaineOperationController extends Controller
{
/**
* #Route("/getById/{id}")
*/
public function getByIdAction($id)
{
$filters = ['id' => $id];
$result = $this->getDoctrine()->getRepository("AcmeBundle:ProchaineOperation")->search($filters);
//process $result
}
/**
* #Route("/getByTitle/{title}")
*/
public function getByTitleAction($title)
{
$filters = ['title' => $title];
$sortBy = 'title';
$result = $this->getDoctrine()->getRepository("AcmeBundle:ProchaineOperation")->search($filters, $sortBy);
//process $result
}
/**
* #Route("/getByIdAndDateMin/{id}/{dateMin}")
*/
public function getByIdAndDateMinAction($id, $dateMin)
{
$filters = ['id' => $id, 'dateMin' => $dateMin];
$sortBy = "dateMin";
$orderBy = "ASC";
$result = $this->getDoctrine()->getRepository("AcmeBundle:ProchaineOperation")->search($filters, $sortBy, $orderBy);
//process $result
}
}
Note that you are calling the same repository method for all controller actions with minor changes according to your parameters. Also note that $sortBy and $orderBy are optionally passed.
Hope it helps!
If your objective is only to query with an AND operator between each properties, the best way could be to use the method proposed by doctrine for that : findBy() cf : this part of the doc
for instance :
$results = $this
->getDoctrine()
->getRepository('AppBundle:ProchaineOperation')
->findBy(array('dateMax' => $myDate, 'title' => 'Hello world');
EDIT : after comment
Then use the same way as Doctrine do : Pass only an array with id, dateMax... as keys if these are set. This should be solve the method signature problem which gives you so much trouble. :)

doctrine: is there a way to use associated entity in findBy

I have an entity 'employee' which is associated to one or more 'manager' entities.
Therefore i use a join table and an association in the employee entity as follows:
/**
* #ManyToMany(targetEntity="manager_entity")
* #JoinTable(name="manager_employees",
* joinColumns={#JoinColumn(name="emp_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="manager_id", referencedColumnName="id", unique=true)}
* )
*/
protected $managers;
this is already working. but now i want to retrieve all employees of a specific manager.
therefore i'm asking if its possible to do something like this:
$mgr = $this->em->getRepository ( 'Entities\manager' )->findOneBy ( array (
"alias" => $this->get('alias'));
// only pseudo code - i know that $managers is a list of managers and $mgr cannot be compared to that
$empList = $this->em->getRepository('Entities\employee')->findBy(array("managers" => $mgr));
Add a function like this to your repository:
public function findByManager($managerId)
{
return $this->getEntityManager()
->createQueryBuilder()
->from('employee', 'e')
->innerJoin('e.managers m')
->where('m.Id = :managerId')
->setParameter('managerId', $managerId)
->getQuery()
->getResult();
}
And then just use:
$employee = $repository->findByManager($manager->getId());

Doctrine2 findby on a Many-to-One mapping

I have two entities with a Unidirectional Many-to-One mapping.
Here's Product:
use Doctrine\Common\Collections\ArrayCollection;
/**
* #Entity
* #Table(name="Product")
* #gedmo:TranslationEntity(class="GPos_Model_Translation_ProductTranslation")
*/
class GPos_Model_Product extends GPos_Doctrine_ActiveEntity {
/**
* #Id #Column(type="integer")
* #GeneratedValue
*/
protected $id;
/**
* #ManyToMany(targetEntity="GPos_Model_Category")
* #JoinTable(name="products_categories",
* joinColumns={#JoinColumn(name="product_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="category_id", referencedColumnName="id")}
* )
*/
protected $categories;
public function __construct() {
$this->categories = new ArrayCollection();
}
public function addCategory(GPos_Model_Category $category) {
if (!$this->categories->contains($category))
$this->categories->add($category);
}
}
As you can see, $categories is an ArrayCollection of GPos_Model_Category entities.
Now what?
Well now I'd like to retrive all products that are in a given category and also all products that are NOT in a given category.
I've tried $products = GPos_Model_Product::findByCategories($category->getId());
but that only gave me
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '1'' at line 1 and $category's ID is 1 so I guess it's not the way to go. Anyone knows how to deal with that ?
Thank you!
I finally found out how to select all products that are in a category thanks to https://stackoverflow.com/a/9808277/1300454.
I tweaked his solution a bit so I could pass an array of Category entities and it would find all products that are within these categories. If you give more than one entity it will return any product that is in at least one of the given categories.
Here's my tweak (I located this function in my Product entity):
/**
*
* Takes an array of GPos_Model_Category entities as parameter and returns all products in these categories
* #param array $categories
*/
public static function findByCategories($categories) {
$categoryArray = array();
foreach ($categories as $category) {
array_push($categoryArray, $category->getId());
}
$qb = Zend_Registry::get('entityManager')->createQueryBuilder();
$qb ->select('p')
->from('GPos_Model_Product', 'p')
->leftJoin('p.categories', 'c')
->andWhere($qb->expr()->in('c.id', $categoryArray));
return $qb->getQuery()->execute();;
}
Here's how you call it:
$products_cat = GPos_Model_Product::findByCategories(array($category));
In this case $category is an entity alone that's why I put it in an array before giving it to the function.
And here is the way you find products that are not in a given category or list of category:
/**
*
* Takes an array of GPos_Model_Category entities as parameter and returns all products not in these categories
* #param array $categories
*/
public static function findByNotCategories($categories) {
$categoryArray = array();
foreach ($categories as $category) {
array_push($categoryArray, $category->getId());
}
$qb = Zend_Registry::get('entityManager')->createQueryBuilder();
$qb2 = Zend_Registry::get('entityManager')->createQueryBuilder();
$qb->select('p')
->from('GPos_Model_Product', 'p')
->where($qb->expr()->notIn('p.id',
$qb2->select('p2.id')
->from('GPos_Model_Product', 'p2')
->leftJoin('p2.categories', 'c')
->andWhere($qb->expr()->in('c.id', $categoryArray))
->getDQL()
));
return $qb->getQuery()->execute();
}
This is actually using a subselect. I'm selecting all products id that are in the given category (that's the subselect) then I'm selecting all products that are not in the result of the subselect. My job here is done!