在zf2中使用DoctrineModule \ Form \ Element \ ObjectSelect时,不显示选项标签

时间:2014-09-16 20:19:49

标签: php doctrine-orm zend-framework2 zend-form

我遇到的问题是,当使用DoctrineModule\Form\Element\ObjectSelect填充选择框时,选项标签为空时选项标签为空。

这是我的实体

namespace Category\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * A Category entity.
 *
 * @ORM\Entity
 * @ORM\Table(name="categories")
 * 
 * @property int $id
 * @property string $name
 * @property string $slug
 * @property string $status
 * 
 */
class Category
{

    const ACTIVE = 1;
    const INACTIVE = 0;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer");
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string");
     */
    protected $name;

    /**
     * @ORM\Column(type="string");
     */
    protected $slug;

    /**
     * @ORM\Column(type="integer");
     */
    protected $status;

    /**
     * @ORM\OneToMany(targetEntity="CategoryAttributes\Entity\CategoryAttributes", mappedBy="category", orphanRemoval=true)
     */
    protected $attributes;

    /*
     * Constructor
     */

    public function __construct() {
        $this->status = self::ACTIVE;
    }

    public function setId($id) {
        $this->id = $id;
    }
    public function getId() {
        $this->id;
    }

    public function setName($name) {
        $this->name = $name;
    }
    public function getName() {
        $this->name;
    }

    public function setSlug($slug) {
        $this->slug = $slug;
    }
    public function getSlug() {
        $this->slug;
    }

    public function setStatus($status) {
        $this->status = $status;
    }
    public function getStatus() {
        $this->status;
    }

    public function getArrayCopy() {
        return get_object_vars($this);
    }

    public function exchangeArray($data) {
        $this->id = (isset($data['id'])) ? $data['id'] : null;
        $this->name = (isset($data['name'])) ? $data['name'] : null;
        $this->slug = (isset($data['slug'])) ? $data['slug'] : null;
        $this->status = (isset($data['status'])) ? $data['status'] : null;
    }

    public function populate($data) {
        $this->id = isset($data['id']) ? $data['id'] : $this->id;
        $this->name = isset($data['name']) ? $data['name'] : $this->name;
        $this->slug = isset($data['slug']) ? $data['slug'] : $this->slug;
        $this->status = isset($data['status']) ? $data['status'] : $this->status;
    }

}

这是我的表格类

namespace CategoryAttributes\Form;

use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Zend\Form\Form;

class CategoryAttributesForm extends Form implements ObjectManagerAwareInterface
{

    protected $objectManager;

    public function __construct(ObjectManager $objectManager) {

        $this->setObjectManager($objectManager);

        parent::__construct('CategoryAttributes');

        $this->add(array(
            'name' => 'name',
            'attributes' => array(
                'type' => 'text',
            ),
            'filters' => array(
                array('StringTrim')
            ),
            'options' => array(
                'required' => true,
                'label' => 'Name',
            )
        ));

        $this->add(array(
            'name' => 'category',
            'type' => 'DoctrineModule\Form\Element\ObjectSelect',
            'attributes' => array(
            ),
            'filters' => array(
                array('StringTrim')
            ),
            'options' => array(
                'required' => true,
                'label' => 'Category',
                'object_manager' => $this->getObjectManager(),
                'target_class' => 'Category\Entity\Category',
                'property' => 'name',
                'empty_option' => '--- select category ---'
            )
        ));
    }

    public function setObjectManager(ObjectManager $objectManager) {
        $this->objectManager = $objectManager;

        return $this;
    }

    public function getObjectManager() {
        return $this->objectManager;
    }

}

问题是这个空的下拉列表。它设置了正确的选项值,但没有设置标签。

enter image description here

1 个答案:

答案 0 :(得分:1)

您没有正确退回'name'属性。

public function getName() {
    $this->name;
}

应该是

public function getName() {
    return $this->name;
}

您还需要将此修复程序应用于其他字段。