I wonder if anyone can help with this Doctrine query.
Basically, My query does not return rows where the foreign key is not set or NULL
. And I would like to return all rows.
Here are 2 schemas
Items
class Items{
/**
* @var integer $id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* @var integer $type
*
* @ManyToOne(targetEntity="Types")
*
*/
private $type;
}
Types
class Types{
/**
* @var integer $id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
}
And the following DQL query
SELECT i.id, i.name, t.name as type FROM entity\Items i, entity\Types t WHERE i.type=t.id (OTHER CONDITIONS...)
That above query does not return the rows that does not have a value in the type
foreign key.
Is it possible to return those rows?
Thank you in advance...
Try a LEFT JOIN:
SELECT i.id, i.name, t.name as type FROM entity\Items i LEFT JOIN i.type t
That will return everything on the left (Items) regardless if there is a matching Type.
Sounds like you do not want this condition in the query;
i.type=t.id
Have you tried removing it or alternative something along the lines
(i.type=t.id OR i.type IS NULL)
Is very simple, you use IS EMPTY
In your entity you have defined a inverse key, then, you call your entity principal where inverseKey IS EMPTY.
SELECT a FROM items a WHERE a.types IS EMPTY;
Then i have a field curriculums is defined like type in the items
/**
* inversed key on principal entity (item)
* @ORM\OneToMany(targetEntity="entity\types", mappedBy="id")
*/
private $types;