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

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());

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');
});
}

Doctrine2 - Doctrine generating query with associated entity - InvalidFieldNameException

Yep, the title suggests: Doctrine is looking for a fieldname that's not there. That's both true and not true at the same time, though I cannot figure out how to fix it.
The full error:
File: D:\path\to\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractMySQLDriver.php:71
Message: An exception occurred while executing 'SELECT DISTINCT id_2
FROM (SELECT p0_.name AS name_0, p0_.code AS code_1, p0_.id AS id_2
FROM product_statuses p0_) dctrn_result ORDER BY p0_.language_id ASC, name_0 ASC LIMIT 25
OFFSET 0':
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'p0_.language_id' in 'order clause'
The query the error is caused by (from error above):
SELECT DISTINCT id_2
FROM (
SELECT p0_.name AS name_0, p0_.code AS code_1, p0_.id AS id_2
FROM product_statuses p0_
) dctrn_result
ORDER BY p0_.language_id ASC, name_0 ASC
LIMIT 25 OFFSET 0
Clearly, that query is not going to work. The ORDER BY should be in the sub-query, or else it should replace p0_ in the ORDER BY with dctrn_result and also get the language_id column in the sub-query to be returned.
The query is build using the QueryBuilder in the indexAction of a Controller in Zend Framework. All is very normal and the same function works perfectly fine when using a the addOrderBy() function for a single ORDER BY statement. In this instance I wish to use 2, first by language, then by name. But the above happens.
If someone knows a full solution to this (or maybe it's a bug?), that would be nice. Else a hint in the right direction to help me solve this issue would be greatly appreciated.
Below additional information - Entity and indexAction()
ProductStatus.php - Entity - Note the presence of language_id column
/**
* #ORM\Table(name="product_statuses")
* #ORM\Entity(repositoryClass="Hzw\Product\Repository\ProductStatusRepository")
*/
class ProductStatus extends AbstractEntity
{
/**
* #var string
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
protected $name;
/**
* #var string
* #ORM\Column(name="code", type="string", length=255, nullable=false)
*/
protected $code;
/**
* #var Language
* #ORM\ManyToOne(targetEntity="Hzw\Country\Entity\Language")
* #ORM\JoinColumn(name="language_id", referencedColumnName="id")
*/
protected $language;
/**
* #var ArrayCollection|Product[]
* #ORM\OneToMany(targetEntity="Hzw\Product\Entity\Product", mappedBy="status")
*/
protected $products;
[Getters/Setters]
}
IndexAction - Removed parts not directly related to QueryBuilder. Added in comments showing params as they are.
/** #var QueryBuilder $qb */
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select($asParam) // 'pro'
->from($emEntity, $asParam); // Hzw\Product\Entity\ProductStatus, 'pro'
if (count($queryParams) > 0 && !is_null($query)) {
// [...] creates WHERE statement, unused in this instance
}
if (isset($orderBy)) {
if (is_array($orderBy)) {
// !!! This else is executed !!! <-----
if (is_array($orderDirection)) { // 'ASC'
// [...] other code
} else {
// $orderBy = ['language', 'name'], $orderDirection = 'ASC'
foreach ($orderBy as $orderParam) {
$qb->addOrderBy($asParam . '.' . $orderParam, $orderDirection);
}
}
} else {
// This works fine. A single $orderBy with a single $orderDirection
$qb->addOrderBy($asParam . '.' . $orderBy, $orderDirection);
}
}
================================================
UPDATE: I found the problem
The above issue is not caused by incorrect mapping or a possible bug. It's that the QueryBuilder does not automatically handle associations between entities when creating queries.
My expectation was that when an entity, such as ProductStatus above, contains the id's of the relation (i.e. language_id column), that it would be possible to use those properties in the QueryBuilder without issues.
Please see my own answer below how I fixed my functionality to be able to have a default handling of a single level of nesting (i.e. ProducStatus#language == Language, be able to use language.name as ORDER BY identifier).
Ok, after more searching around and digging into how and where this goes wrong, I found out that Doctrine does not handle relation type properties of entities during the generation of queries; or maybe does not default to using say, the primary key of an entity if nothing is specified.
In the use case of my question above, the language property is of a #ORM\ManyToOne association to the Language entity.
My use case calls for the ability to handle at lease one level of relations for default actions. So after I realized that this is not handled automatically (or with modifications such as language.id or language.name as identifiers) I decided to write a little function for it.
/**
* Adds order by parameters to QueryBuilder.
*
* Supports single level nesting of associations. For example:
*
* Entity Product
* product#name
* product#language.name
*
* Language being associated entity, but must be ordered by name.
*
* #param QueryBuilder $qb
* #param string $tableKey - short alias (e.g. 'tab' with 'table AS tab') used for the starting table
* #param string|array $orderBy - string for single orderBy, array for multiple
* #param string|array $orderDirection - string for single orderDirection (ASC default), array for multiple. Must be same count as $orderBy.
*/
public function createOrderBy(QueryBuilder $qb, $tableKey, $orderBy, $orderDirection = 'ASC')
{
if (!is_array($orderBy)) {
$orderBy = [$orderBy];
}
if (!is_array($orderDirection)) {
$orderDirection = [$orderDirection];
}
// $orderDirection is an array. We check if it's of equal length with $orderBy, else throw an error.
if (count($orderBy) !== count($orderDirection)) {
throw new \InvalidArgumentException(
$this->getTranslator()->translate(
'If you specify both OrderBy and OrderDirection as arrays, they should be of equal length.'
)
);
}
$queryKeys = [$tableKey];
foreach ($orderBy as $key => $orderParam) {
if (strpos($orderParam, '.')) {
if (substr_count($orderParam, '.') === 1) {
list($entity, $property) = explode('.', $orderParam);
$shortName = strtolower(substr($entity, 0, 3)); // Might not be unique...
$shortKey = $shortName . '_' . (count($queryKeys) + 1); // Now it's unique, use $shortKey when continuing
$queryKeys[] = $shortKey;
$shortName = strtolower(substr($entity, 0, 3));
$qb->join($tableKey . '.' . $entity, $shortName, Join::WITH);
$qb->addOrderBy($shortName . '.' . $property, $orderDirection[$key]);
} else {
throw new \InvalidArgumentException(
$this->getTranslator()->translate(
'Only single join statements are supported. Please write a custom function for deeper nesting.'
)
);
}
} else {
$qb->addOrderBy($tableKey . '.' . $orderParam, $orderDirection[$key]);
}
}
}
It by no means supports everything the QueryBuilder offers and is definitely not a final solution. But it gives a starting point and solid "default functionality" for an abstract function.

Doctrine: Removing entity in self-referencing (many-to-many)

I would like to ask your help in deleting association.
My User entity:
class User
{
...
/**
* #ORM\ManyToMany(targetEntity="User", mappedBy="following")
**/
private $followers;
/**
* #ORM\ManyToMany(targetEntity="User", inversedBy="followers")
* #ORM\JoinTable(name="friends",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="friend_user_id", referencedColumnName="id")}
* )
**/
private $following;
I have two actions:
Profile:follow
// followAction
$entityManager = $this->getDoctrine()->getEntityManager();
$me->addFollowing($targetUser);
$targetUser->addFollower($me);
$entityManager->persist($me);
$entityManager->persist($targetUser);
$entityManager->flush();
Profile:unfollow
$entityManager = $this->getDoctrine()->getEntityManager();
$me->removeFollowing($targetUser);
$targetUser->removeFollower($me);
$entityManager->persist($me);
$entityManager->persist($targetUser);
$entityManager->flush();
Process of following is working in a proper way, and I see appropriate records friends table.
But when I am trying to unfollow user, I receive exception:
An exception occurred while executing 'INSERT INTO friends (user_id, friend_user_id) VALUES (?, ?)' with params {"1":2,"2":10}:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2-10' for key 'PRIMARY'
What am I doing wrong? I've tried with persist and without it, the same. Maybe something in association configs?
Your first mistake is 2 persist actions, you only need one. Check this:
// class User
public function switchFollowingUser(User $user)
{
if ( $this->following->contains($user) )
$this->following->removeElement($user) ;
else
$this->following->add($user) ;
}
and controller would be just
$follower->switchFollowingUser($user) ;
Extract this method into two methods if you want but I kinda prefer this way because it is shorter.
Second thing:
Did you put
$this->following = new ArrayCollection() ;
$this->followers = new ArrayCollection() ;
in __construct() ?
Try if this works.

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!

Doctrine 2 - Insert new item in database

I'm trying to make something very simple.. but I do wrong, and I don't know what is the problem. Just I'm trying to insert new item to database with Doctrine 2:
$favouriteBook = new UserFavouriteBook;
$favouriteBook->user_id = 5;
$favouriteBook->book_id = 8;
$favouriteBook->created_at = new DateTime("now");
$this->_em->persist($favouriteBook);
$this->_em->flush();
As you can see.. is very simple, but that, give me next error:
Error: Message: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null
Obviosly, if I make a "dump" before "persist" and "flush" of $favouriteBook, all looks be correct..
This is my "favouriteBook" entity:
/** #Column(type="integer")
* #Id
*/
private $user_id;
/** #Column(type="integer")
* #Id
*/
private $book_id;
/**
* #ManyToOne(targetEntity="Book", inversedBy="usersFavourite")
* #JoinColumn(name="book_id", referencedColumnName="id")
*/
private $book;
/**
* #ManyToOne(targetEntity="User", inversedBy="favouriteBooks")
* #JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/** #Column(type="datetime") */
private $created_at;
public function __get($property) {
return $this->$property;
}
public function __set($property, $value) {
$this->$property = $value;
}
Anyone can image what is the problem? .. I don't know what else try.. Thanks
I think what beberlei is saying is that within your favouriteBook entity, you don't need to define the user_id and book_id as class properties, b/c the book and user properties you have set already recognize these as the relevant join columns. Also, your attempt to persist the favouriteBook entity failed because you need to set the book and user entity associations within the favouriteBook entity, not the foreign keys. So it would be:
$favouriteBook = new UserFavouriteBook;
$favouriteBook->book = $book;
$favouriteBook->user = $user;
$favouriteBook->created_at = new DateTime("now");
$this->_em->persist($favouriteBook);
$this->_em->flush();
You are mapping foreign keys and the associations. You have to modify the association not the foreign key field. Its bad-practice to map them both, you should remove $book_id and $user_id completly.