doctrine-ormzend-framework3

Doctrine Orm - Searching embedded associations


I have an Asset Entity that uses an embedded association:

/**
 * @ORM\Entity
 */
class Asset
{

....

    /**
     * @ORM\Embedded(class="Money\Money")
     */
   private $code;

I want to search this class and my first instinct was to do something like this:

public function findOneByCurrencyCode(string $currencyCode)
{
    $qb = $this->assetRepository->createQueryBuilder('asset');
    $qb->innerJoin('asset.code', 'code')
        ->where('code.currency = :currency')
        ->setParameter('currency', $currencyCode);

    $result = $qb->getQuery()->getOneOrNullResult();
    return $result;
}

This, however, returns the following:

[Semantical Error] line 0, col 65 near 'code WHERE code.currency': Error: 
Class Domain\Asset\Asset has no association named code

How do you search embedded classes?

EDIT:

I can get a result by doing something like this, however, this I feel is a hack:

    $query = "SELECT * from asset where code_currency='BTC';";
    $statement = $this->objectManager->getConnection()->prepare($query);
    $statement->execute();

    $result = $statement->fetchAll();

    return $result;

Solution

  • "Embedded associations" does NOT exist. So, you doesn't need JOINS.

    An embedded is part of your entity along with the others properties.

    You have to do something like that:

    SELECT u
    FROM User u
    WHERE u.address.city
    

    (In this query, your entity is User, your embedded is Address and *city is the property of your embedded).

    It's pretty good explained in Doctrine documentation: Doctrine embeddables