I'm trying to grab some data from an table which works fine as long as I don't use
->andwhere('s.client_id = :clientid')
->setParameter('clientid', $this->clientId)
I f use the two line above to locate only neccessary data, I end up in a error 500 :(:(
The entity looks like:
class SanitationType
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Pr\UserBundle\Entity\Client")
* @ORM\JoinColumn(name="client_id", referencedColumnName="id")
*/
private $client;
/**
* @ORM\Column(type="string", length=20)
* @Gedmo\Translatable
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Gedmo\Translatable
*/
private $description;
/**
* @ORM\Column(name="`enabled`", type="boolean")
*/
private $enabled;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $created_by;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $created;
..............
and my script to grab the data inside the controller looks like this:
$query = $em->createQueryBuilder()
->select('s')
->from('PrSensorBundle:SanitationType', 's')
->where('s.enabled = 1')
->andwhere('s.client_id = :client_id')
->setParameter('client_id', $this->clientId)
->orderBy('s.name', 'ASC')
->getQuery();
$results=$query->getArrayResult();
I don't see any error but it is not working at all :(:(
Do I forgot something?
Couple of things. First, it's not clear what $this->clientId
refers to, but if you're trying to reference the id of the client object associated with a SanitationType object, then you'd need to have a public getClient()
method on the SanitationType class and a getId()
method on the Client class. So obtaining the client id from a SanitationType object $sanitationType
then becomes:
$sanitationType->getClient()->getId()
Second, there is no client_id
property in the SanitationType class. Doctrine sees the properties of a class as you've defined them. So in this case, to look up a SanitationType object(s) in the database by the id of a Client association(s), you would need to perform an inner join. Your query builder would look like this:
$query = $em->createQueryBuilder()
->select('s')
->from('PrSensorBundle:SanitationType', 's')
->innerJoin('s.client', 'sc')
->where('s.enabled = 1')
->andwhere('sc.id = :client_id')
->setParameter('client_id', $this->clientId)
->orderBy('s.name', 'ASC')
->getQuery();
$results=$query->getArrayResult();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments