Sezgin Sezgin - 3 months ago 31
PHP Question

zf2 fieldset ArrayCollection is empty while it is expected to be filled

I have a form ProductForm and inside that form I called base fieldset ProductFieldset and inside ProductFieldset I called CategoryFieldset. The relationship between the entities is OneToOne.

Here are the Entities:

namespace Trunk\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity
*/
class Product
{
/**
* @var integer
*
* @ORM\Column(name="Id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;

/**
* @var integer
*
* @ORM\Column(name="CategoryId", type="integer", nullable=false)
*/
protected $categoryid;

/**
* @ORM\OneToOne(targetEntity="Trunk\Entity\Category", mappedBy="product")
* @ORM\JoinColumn(name="categoryid", referencedColumnName="id")
*/
protected $category;

public function __construct() {
$this->category = new ArrayCollection();
}

/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}

/**
* @return int
*/
public function getCategoryid()
{
return $this->categoryid;
}

/**
* @param int $categoryid
*/
public function setCategoryid($categoryid)
{
$this->categoryid = $categoryid;
}

public function addCategory(Category $category)
{
if (! $this->category->contains($category)) {
$this->category->add($category);
}
return $this;
}
public function removeCategory(Category $category)
{
if ($this->category->contains($category)) {
$this->category->removeElement($category);
}
return $this;
}

public function getCategory()
{
return $this->category;
}
}


And here is the Category entity:

namespace Trunk\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Category
*
* @ORM\Table(name="category")
* @ORM\Entity
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="Id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var integer
*
* @ORM\Column(name="ParentId", type="integer", nullable=false)
*/
private $parentid = '0';

/**
* @var string
*
* @ORM\Column(name="Title", type="string", length=255, nullable=false)
*/
private $title;

/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}

/**
* Set title
*
* @param string $title
*
* @return Category
*/
public function setTitle($title)
{
$this->title = $title;

return $this;
}

/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}


The result after the submission of the form is:

object(Trunk\Entity\Product)[480]
protected 'id' => null
protected 'categoryid' => null
protected 'category' =>
object(Doctrine\Common\Collections\ArrayCollection)[482]
private 'elements' =>
array (size=0)
empty


I have used Doctrine hydrators however there is no results.

Could you give me some ideas why is that always empty ? Thank you in advance!


More code


$form = $this->form;
$form->setAttribute('action', $this->url . '/admin/holiday/add/bg');
$form->prepare();
echo $this->form()->openTag($form);
$holiday = $form->get('product');
echo $this->formElement($holiday->get('category')->get('categoryid'));
echo $this->form()->closeTag();


Here it is the add action in the controller:

public function addAction()
{
$oRequest = $this->getRequest();

if ($oRequest->isPost()) {
$aPost = $oRequest->getPost('product');

$this->productForm->setData($oRequest->getPost());

if ($this->productForm->isValid()) {

try {
$result = $this->productService->addProduct($this->productForm->getData());

if ($result) {
return $this->redirect()->toRoute('trunk', array('controller' => 'product', 'action' => 'edit', 'id' => $result->getLang(), 'param' => $result->getId()));
}
} catch (\Exception $e) {
// some DB error happened,, log it and let the user know
exit;
}
}
}

return new ViewModel(array(
'form' => $this->productForm,
));


}

The code below is used to hydrate the objects in the CategoryFieldset

$this->setHydrator(new DoctrineHydrator($this->getObjectManager(), 'Trunk\Entity\Category'))
->setObject(new Category());


And here it is how I add CategoryFieldset in the ProductFieldset

$this->add(array(
'type' => 'Trunk\Form\CategoryFieldset',
'name' => 'category'
));


Edit 1:

ManyToOne relationship in ProductEntity

/**
* @ORM\ManyToOne(targetEntity="Trunk\Entity\Category", inversedBy="products")
* @ORM\JoinColumn(name="categoryid", referencedColumnName="id")
*/
protected $category;


OneToMany relationship in CategoryEntity

/**
* @ORM\OneToMany(targetEntity="Trunk\Entity\Product", mappedBy="category")
*/
protected $products;


I got the following error


Could not resolve type of column "id" of class "Trunk\Entity\Category"


I checked the code in PersistHelper.php getTypeOfColumn function and I found out that it is looking for joinColumns.

Answer

The question is resolved!

To fix the relationship error that I posted last I had to use this annotation in the ProductEntity

/**
 * @ORM\ManyToOne(targetEntity="Trunk\Entity\Category", inversedBy="products")
 * @ORM\JoinColumn(name="CategoryId", referencedColumnName="Id")
 */
protected $category;

In another words I changed the reference column name from "id" to Id(case sensitive).

I hope this question and examples will help out other colleagues as well.