首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用DoctrineHydrator在ZendFramework2表单和字段集中使用Doctrine 2

使用DoctrineHydrator在ZendFramework2表单和字段集中使用Doctrine 2
EN

Stack Overflow用户
提问于 2014-04-24 15:19:17
回答 1查看 773关注 0票数 0

客户实体

代码语言:javascript
复制
<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Client
 *
 * @ORM\Table(name="client", uniqueConstraints={@ORM\UniqueConstraint(name="mail", columns=    
 * {"mail"}), @ORM\UniqueConstraint(name="pseudo", columns={"pseudo"})}, indexes=
 * {@ORM\Index(name="FK_client_situation", columns={"situation"}),   
 *@ORM\Index(name="FK_client_city", columns={"ville"})})
 * @ORM\Entity
 */
class Client
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="birthay", type="datetime", nullable=true)
     */
    private $birthay;

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

    /**
     * @var string
     *
     * @ORM\Column(name="phone", type="text", nullable=true)
     */
    private $phone;

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

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

    /**
     * @var integer
     *
     * @ORM\Column(name="situation", type="integer", nullable=true)
     */
    private $situation;

    /**
     * @var integer
     *
     * @ORM\Column(name="ville", type="integer", nullable=true)
     */
    private $ville;

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

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

    /**
     * @var integer
     *
     * @ORM\Column(name="newseltter", type="integer", nullable=true)
     */
    private $newseltter;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="dateinscription", type="datetime", nullable=true)
     */
    private $dateinscription;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="datedelete", type="datetime", nullable=true)
     */
    private $datedelete;

    /**
     * @var string
     *
     * @ORM\Column(name="statut", type="string", length=50, nullable=true)
     */
    private $statut;

    /**
     * @var string
     *
     * @ORM\Column(name="contenu", type="text", nullable=true)
     */
    private $contenu;



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

    /**
     * Set firstname
     *
     * @param string $firstname
     * @return Client
     */
    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;

        return $this;
    }

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

    /**
     * Set lastname
     *
     * @param string $lastname
     * @return Client
     */
    public function setLastname($lastname)
    {
        $this->lastname = $lastname;

        return $this;
    }

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

    /**
     * Set birthay
     *
     * @param \DateTime $birthay
     * @return Client
     */
    public function setBirthay($birthay)
    {
        $this->birthay = $birthay;

        return $this;
    }

    /**
     * Get birthay
     *
     * @return \DateTime 
     */
    public function getBirthay()
    {
        return $this->birthay;
    }

    /**
     * Set mail
     *
     * @param string $mail
     * @return Client
     */
    public function setMail($mail)
    {
        $this->mail = $mail;

        return $this;
    }

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

    /**
     * Set phone
     *
     * @param string $phone
     * @return Client
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

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

    /**
     * Set pseudo
     *
     * @param string $pseudo
     * @return Client
     */
    public function setPseudo($pseudo)
    {
        $this->pseudo = $pseudo;

        return $this;
    }

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

    /**
     * Set password
     *
     * @param string $password
     * @return Client
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

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

    /**
     * Set situation
     *
     * @param integer $situation
     * @return Client
     */
    public function setSituation($situation)
    {
        $this->situation = $situation;

        return $this;
    }

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

    /**
     * Set ville
     *
     * @param integer $ville
     * @return Client
     */
    public function setVille($ville)
    {
        $this->ville = $ville;

        return $this;
    }

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

    /**
     * Set facebook
     *
     * @param string $facebook
     * @return Client
     */
    public function setFacebook($facebook)
    {
        $this->facebook = $facebook;

        return $this;
    }

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

    /**
     * Set picture
     *
     * @param string $picture
     * @return Client
     */
    public function setPicture($picture)
    {
        $this->picture = $picture;

        return $this;
    }

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

    /**
     * Set newseltter
     *
     * @param integer $newseltter
     * @return Client
     */
    public function setNewseltter($newseltter)
    {
        $this->newseltter = $newseltter;

        return $this;
    }

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

    /**
     * Set dateinscription
     *
     * @param \DateTime $dateinscription
     * @return Client
     */
    public function setDateinscription($dateinscription)
    {
        $this->dateinscription = $dateinscription;

        return $this;
    }

    /**
     * Get dateinscription
     *
     * @return \DateTime 
     */
    public function getDateinscription()
    {
        return $this->dateinscription;
    }

    /**
     * Set datedelete
     *
     * @param \DateTime $datedelete
     * @return Client
     */
    public function setDatedelete($datedelete)
    {
        $this->datedelete = $datedelete;

        return $this;
    }

    /**
     * Get datedelete
     *
     * @return \DateTime 
     */
    public function getDatedelete()
    {
        return $this->datedelete;
    }

    /**
     * Set statut
     *
     * @param string $statut
     * @return Client
     */
    public function setStatut($statut)
    {
        $this->statut = $statut;

        return $this;
    }

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

    /**
     * Set contenu
     *
     * @param string $contenu
     * @return Client
     */
    public function setContenu($contenu)
    {
        $this->contenu = $contenu;

        return $this;
    }

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

?>

ClienFieldset

代码语言:javascript
复制
<?php
namespace Application\Form;


use Application\Entity\Client;


use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;



class ClientFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('post');


        //$em = Registry::get('entityManager');

        $this->setHydrator(new DoctrineHydrator($objectManager,'Application\Entity\Client'))
            ->setObject(new Client());

        $this->setLabel('Post');

        $this->add(array(
            'name' => 'id',
            'attributes' => array(
                'type' => 'hidden'
            )
        ));

        $this->add(array(
            'name' => 'mail',
            'options' => array(
                'label' => 'Title for this Post'
            ),
            'attributes' => array(
                'type' => 'text'
            )
        ));

        $this->add(array(
            'name' => 'password',
            'options' => array(
                'label' => 'Text-Content for this post'
            ),
            'attributes' => array(
                'type' => 'text'
            )
        ));
    }

    /**
     * Define InputFilterSpecifications
     *
     * @access public
     * @return array
     */
    public function getInputFilterSpecification()
    {
        return array(
            'mail' => array(
                'required' => true,
                'filters' => array(
                    array('name' => 'StringTrim'),
                    array('name' => 'StripTags')
                ),
                'properties' => array(
                    'required' => true
                )
            ),
            'password' => array(
                'required' => true,
                'filters' => array(
                    array('name' => 'StringTrim'),
                    array('name' => 'StripTags')
                ),
                'properties' => array(
                    'required' => true
                )
            )
        );
    }
}

?>

客户表格

代码语言:javascript
复制
<?php
namespace Application\Form;

use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Form;

class ClientForm extends Form
{

        public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('post');

        // The form will hydrate an object of type "BlogPost"
        $this->setHydrator(new DoctrineHydrator($objectManager));

        // Add the user fieldset, and set it as the base fieldset
        $clientFieldset = new ClientFieldset($objectManager);
        $clientFieldset->setUseAsBaseFieldset(true);
        $this->add($clientFieldset);




        $this->add(array(
            'name' => 'security',
            'type' => 'Zend\Form\Element\Csrf'
        ));

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type'  => 'submit',
                'value' => 'Go',
                'id'    => 'submitbutton'
            )
        ));

        $this->setValidationGroup(array(
            'security',
            'post' => array(
                'title',
                'text'
            )
        ));
    }
}

?>

指数作用

代码语言:javascript
复制
<?php

 public function indexAction()
    {
        $objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');

        // Create the form and inject the ObjectManager
        $form = new ClientForm($objectManager);

        // Create a new, empty entity and bind it to the form
        $Client = new Client();
        $form->bind($Client);


        if ($this->request->isPost()) {
            $form->setData($this->request->getPost());

            if ($form->isValid()) {
                $objectManager->persist($Client);
                $objectManager->flush();
            }
        }
        return array('form' => $form);


    }
?>
#index View

<table class="table">
    <?php
    if($this->form){
        echo $this->form()->openTag($form);
        echo $this->formRow($form->get('id'));

        echo $this->formRow($form->get('mail'));

        echo $this->form()->closeTag();
    }

</table> ?>

当我执行我的代码时,我会得到这个错误。

警告: include(/var/www/Colocation/Colocation/module/Visitor/config/../view/error/index.phtml):未能打开流:在第506行的/var/www/Colocation/Colocation/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php中没有这样的文件或目录 警告: include():打开'/var/www/Colocation/Colocation/module/Visitor/config/../view/error/index.phtml‘以便将(include_path='.:/usr/share/php:/usr/share/pear')包含在第506行的/var/www/Colocation/Colocation/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php中失败

救救我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-25 22:27:34

php警告说它找不到文件错误/index.phtml。

它正在查找此文件,因为在代码中的某个地方有一个错误,这将导致错误页。

默认错误页文件位于应用程序模块( module / Application /view/ error /)中,但在您的示例中,它试图在“访问者”模块中找到这些文件。

也许在“访问者”模块中,您覆盖了应用程序的模块“view_manager”信任,因此您可以尝试以下内容之一:

  • 打开“访问者”模块配置文件,注释或删除所有与错误相关的配置,使用此配置为“view_manager”就足够了 'view_manager' => array( 'template_path_stack' => array( 'visitor' => __DIR__ . '/../view', ), ),
  • 在“访问者”模块中创建错误页面模板,就像您可以在应用程序模块中找到的模板一样。只有在您想要自定义错误页模板、保持应用程序错误模板不变的情况下才这样做,否则使用第一个解决方案。

完成后,您应该能够看到另一个错误,但这一次与您的代码相关!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23273073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档