I have entity Product and entity Subcategory:
Subcategory.php
namespace Project\Entities;
/**
* Subcategory
* @Entity
* @Table(name="subcategories")
*
*/
class Subcategory {
/**
*
* @Id
* @GeneratedValue
* @Column(type="integer")
* @var integer $id
*/
public $id;
/**
* @Column(type="string")
* @var string $name
*/
public $name;
}
Product.php
namespace Project\Entities;
/**
*
* Product
* @Entity
* @Table(name="products")
*/
class Product {
/**
*
* @Id
* @GeneratedValue
* @Column(type="integer",length=16)
* @var integer $id
*/
public $id;
/**
*
* @Column(type="integer",length=3)
* @var integer $id_subcat
*/
public $id_subcat;
/**
*
* @Column(type="string")
* @var integer $name
*/
public $name;
/**
*
* @Column(type="string")
* @var integer $available
*/
public $available;
}
I would like to get all Subcategories that have Products available field equal 1.
Ergo. Get Subcategories that have only available products in them.
I know how to write SQL query for that but I don’t know how to achieve this in DQL.
Should I provide some additional annotation?
You have to begin by declaring the relationship in your entities. I am going to assume that your Subcategory can have many Products (one-to-many):
I haven’t tested this code obviously, so I apologize if there are some errors, this is off the top of my head mostly.
** Then to query, in SF2, it would be like this:
Now, because we have protected variables in our entity, you’d want setters and getters on your objects.
This can be very helpful for formatting how content is returned, etc.