Reputation: 400
I have problems with my classes Symfony2.
I have 3 classes "Niveau, Classe and Serie" that here. A class is related to a level and against a Series by Series Level and are in contact with several class. Here is the source code
/**
* KS\SchoolBundle\Entity\Niveau
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="KS\SchoolBundle\Entity\NiveauRepository")
*/
class Niveau
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $niveau
*
* @ORM\Column(name="niveau", type="string", length=255)
*/
private $niveau;
/**
* @var string $code
*
* @ORM\Column(name="code", type="string", length=255)
*/
private $code;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set niveau
*
* @param string $niveau
* @return Niveau
*/
public function setNiveau($niveau)
{
$this->niveau = $niveau;
return $this;
}
/**
* Get niveau
*
* @return string
*/
public function getNiveau()
{
return $this->niveau;
}
/**
* Set code
*
* @param string $code
* @return Niveau
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* @return string
*/
public function getCode()
{
return $this->code;
}
}
/**
* KS\SchoolBundle\Entity\Serie
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="KS\SchoolBundle\Entity\SerieRepository")
*/
class Serie
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $serie
*
* @ORM\Column(name="serie", type="string", length=255)
*/
private $serie;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $serie
* @return Serie
*/
public function setSerie($serie)
{
$this->serie = $serie;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getSerie()
{
return $this->seriee;
}
}
/**
* KS\SchoolBundle\Entity\Classe
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="KS\SchoolBundle\Entity\ClasseRepository")
*/
class Classe
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @var type integer
*
* @ORM\ManyToOne(targetEntity="KS\SchoolBundle\Entity\Niveau")
* @ORM\JoinColumn(nullable=false)
*/
private $niveau;
/**
*
* @var type integer
*
* @ORM\ManyToOne(targetEntity="KS\SchoolBundle\Entity\Serie")
* @ORM\JoinColumn
*/
private $serie;
/**
*
* @var type string
*
* @ORM\ManyToOne(targetEntity="KS\SchoolBundle\Entity\Section", inversedBy="classes")
* @ORM\JoinColumn(nullable=false)
*/
private $section;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set niveau
*
* @param KS\School\Entity\Niveau $niveau
* @return Classe
*/
public function setNiveau(\KS\School\Entity\Niveau $niveau)
{
$this->niveau = $niveau;
return $this;
}
/**
* Get niveau
*
* @return KS\School\Entity\Niveau
*/
public function getNiveau()
{
return $this->niveau;
}
/**
* Set serie
*
* @param KS\School\Entity\Serie $serie
* @return Classe
*/
public function setSerie(\KS\School\Entity\Serie $serie)
{
$this->serie = $serie;
return $this;
}
/**
* Get serie
*
* @return KS\School\Entity\Serie
*/
public function getSerie()
{
return $this->serie;
}
/**
* Set section
*
* @param KS\School\Entity\Section $section
* @return Classe
*/
public function setSection(\KS\School\Entity\Section $section)
{
$this->section = $section;
return $this;
}
/**
* Get section
*
* @return KS\School\Entity\Section
*/
public function getSection()
{
return $this->section;
}
public function __toString() {
return $this->getNiveau() . ' ' . $this->getSerie();
}
}
My problem is that entity in a field I want to display a dropdown that has value as a concatenation of the code level and series of Series I which procedé as follows
->add('class', 'entity', array(
'class' => 'KSSchoolBundle:Classe',
'property' => 'value',
'query_builder' => function (\KS\SchoolBundle\Entity\ClasseRepository $er) {
$results = $er->findAllClassesByCodeAndSerie();
$data = array();
foreach ($results as $result) {
$data[] = array(
'id' => $result['id'],
'value' => $result['code'] . ' ' . is_null($result['serie']) ? '' : $result['serie'],
);
}
return $data;
},
)
)
but nothing since there. query_builder return $data that is as you see an array. id must be the value of the option tag value and value should be what the user must see, but I do not see how to do this. The browser give this error Expected argument of type "Doctrine\ORM\QueryBuilder", "array" given
I wonder if that would be a way to do it right the Class class at the __toString () whereby it returns something like getNiveau (). getCode (). ''. getSerie (). getSerie ()
Please one hand it's been two days that I am on this form.
Upvotes: 0
Views: 2952
Reputation: 52483
You return an array instead of a QueryBuilder instance in your options array for the entity field type named 'class'.
The wrong part is this one:
'query_builder' => function (\KS\SchoolBundle\Entity\ClasseRepository $er) {
// this is wrong as it does not return a Query but your Class entities themself ...
// findByCodeAndSerie would normally receive 2 arguments: $code & $serie
// but anyway ... this does not belong here !
$results = $er->findAllClassesByCodeAndSerie();
// ... weird stuff
$data = array();
// ... more weird stuff
return $data; /// you return an array instead of QueryBuilder
a correct implementation would be something like this:
use KS\SchoolBundle\Entity\ClasseRepository;
// ...
// 'property' => 'value', // remove this !!
'query_builder' => function(ClasseRepository $er) {
return $er->createQueryBuilder('c')
->orderBy('c.code', 'ASC');
},
now just add the __toString() method to your Class entity. This will be the rendered label for the entity:
public function __toString()
{
return $this->niveau . $this->code . ' ' . $this->serie . $this->serie;
}
Read the enitity field type reference for more information and examples.
Upvotes: 1