0

目前我正在尝试将教义 2 集成到 codeigniter 2 中,我正在按照以下教程进行所有这些操作。

http://www.joelverhagen.com/blog/2011/05/setting-up-codeigniter-2-with-doctrine-2-the-right-way/

根据我的教程

1)Doctrine 2 in  library
2)Doctrine.php file in  library
3)doctrine-cli.php in root folder
4)yaml file inside applications/models/mappings and also entities and proxies inside the same location

根据命令提示符下的教程,我已将实体设为用户实体

<?php

 namespace Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * Entities\User
 */
class User
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var string $password
     */
    private $password;

    /**
     * @var string $firstName
     */
    private $firstName;

    /**
     * @var string $lastName
     */
    private $lastName;

    /**
     * @var string $email
     */
    private $email;

    /**
     * @var string $website
     */
    private $website;

    /**
     * @var datetime $created
     */
    private $created;


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

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

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

    /**
     * Set firstName
     *
     * @param string $firstName
     * @return User
     */
    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 User
     */
    public function setLastName($lastName)
    {
        $this->lastName = $lastName;
        return $this;
    }

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

    /**
     * Set email
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }

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

    /**
     * Set website
     *
     * @param string $website
     * @return User
     */
    public function setWebsite($website)
    {
        $this->website = $website;
        return $this;
    }

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

    /**
     * Set created
     *
     * @param datetime $created
     * @return User
     */
    public function setCreated($created)
    {
        $this->created = $created;
        return $this;
    }

    /**
     * Get created
     *
     * @return datetime 
     */
    public function getCreated()
    {
        return $this->created;
    }
}?>

文章实体也为

<?php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * Entities\Article
 */
class Article
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var string $title
     */
    private $title;

    /**
     * @var text $content
     */
    private $content;

    /**
     * @var datetime $created
     */
    private $created;

    /**
     * @var Entities\User
     */
    private $user;


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

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

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

    /**
     * Set content
     *
     * @param text $content
     * @return Article
     */
    public function setContent($content)
    {
        $this->content = $content;
        return $this;
    }

    /**
     * Get content
     *
     * @return text 
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * Set created
     *
     * @param datetime $created
     * @return Article
     */
    public function setCreated($created)
    {
        $this->created = $created;
        return $this;
    }

    /**
     * Get created
     *
     * @return datetime 
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set user
     *
     * @param Entities\User $user
     * @return Article
     */
    public function setUser(\Entities\User $user = null)
    {
        $this->user = $user;
        return $this;
    }

    /**
     * Get user
     *
     * @return Entities\User 
     */
    public function getUser()
    {
        return $this->user;
    }
}

我已经按照该教程中的说明做了所有事情,但是当我尝试在数据库表中插入值时,我得到了错误

Fatal error: Class 'Entities' not found in D:\xampp\htdocs\ci_doctrine\application\controllers\welcome.php on line 32

当我使用欢迎控制器插入数据时,我的控制器是

class Welcome extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */


    public function __construct() {
        parent::__construct();

      // $this->load->library('Doctrine.php');
        //$this->em = $this->doctrine->em;
    }

public function index()
{
    // create a new user object
    $user = new Entities/User;
    $user->setFirstName('Joel');
    $user->setLastName('Verhagen');
    $user->setPassword(md5('Emma Watson'));
    $user->setEmail('joel@joelverhagen.com');
    $user->setWebsite('http://www.joelverhagen.com');
    $user->setCreated(new DateTime());

    // standard way in CodeIgniter to access a library in a controller: $this->library_name->member->memberFunction()
    // save the object to database, so that it can obtain its ID
    $this->doctrine->em->persist($user);

    // create a new article object
    $article = new Entities/Article;
    $article->setTitle('Emma Watson is extremely talented, no?');
    $article->setContent('By talented, I mean good at lookin\' good.');
    $article->setCreated(new DateTime());
    // the user object you pass must be persisted first!
    $article->setUser($user);

    // save the article object to the database
    $this->doctrine->em->persist($article);
    $this->doctrine->em->flush();

    echo "<b>Success!</b>";

}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

发生了什么我不知道..任何建议和帮助..??

4

1 回答 1

1

您不使用完全限定的名称空间,而是使用相对名称空间。

http://php.net/manual/en/language.namespaces.basics.php

这就是为什么。

尝试

$article = new \Entities\Article;

在您的控制器中。

编辑:哦,甚至更好。您试图与Entities分开Article

您使用/的是\.

于 2013-07-17T08:38:20.697 回答