【问题标题】:Codeigniter doctrineCodeigniter 学说
【发布时间】:2017-04-09 13:03:00
【问题描述】:

我尝试将教义与 Codeigniter 结合使用。

我在数据库中有两个表,作者和书籍。

Authors 表具有字段 id、name 和 lastname。 Books 表有字段 id、title 和 author_id。

我知道如何将数据写入表格,但我不知道如何获取该作者撰写的所有作者和书籍的列表。有人可以帮我吗?

作者模型

<?php
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @Entity @Table(name="authors")
 **/
class Author
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;
    /** @Column(type="string") **/
    protected $name;
    /** @Column(type="string") **/
    protected $lastname;

    /**
     * @OneToMany(targetEntity="Book", mappedBy="author")
     */

    private $books;

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

    public function setAuthor($name,$lastname)
    {
        $this->name = $name;
        $this->lastname = $lastname;
    }

    public function authorName()
    {
        return $this->name.' '.$this->lastname;
    }

    public function updateAuthor($name,$lastname)
    {
        $this->name = $name;
        $this->lastname = $lastname;
    }

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

    public function setLastName($lastname)
    {
        $this->lastname = $lastname;
    }

    public function getBooks()
    {
        return $this->books;
    }

}

图书模型

<?php
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @Entity @Table(name="books")
 **/
class Book
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;
    /** @Column(type="string") **/
    protected $title;

    /**
     * @ManyToOne(targetEntity="Author", inversedBy="books")
     * @oinColumn(name="author_id", referencedColumnName="id")
     */

    private $author;

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function setAuthor($author)
    {
        $this->author = $author;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function getAuthor()
    {
        return $this->author;
    }
}

提前致谢

【问题讨论】:

    标签: php mysql codeigniter doctrine-orm


    【解决方案1】:

    你有一个实体类 Author 和 Book。所以你可以使用这个代码。您可以根据需要进行修改。

    $authors = $em->getRepository("Author")->findAll();
    

    现在您可以通过调用 getBooks() 来获取特定作者的书籍 例如:

    $authors = $em->getRepository("Author")->findAll();//$em is entity manager
        if (count($authors)):
            foreach ($authors as $author):
                $books= $author->getBooks(); //all books of current author
            endforeach;
        endif;
    

    建议:您应该在实体中定义命名空间。

    【讨论】:

      【解决方案2】:

      我正在为您提供一个使用 queryBuilder 的选择查询模型。尝试执行它。

          $query= $this->doctrine->em->createQueryBuilder();
          $query->select('bk.id book_id,au.id author_id');
          $query->add('from', 'Entities\Authors au');
          $query->Join('Entities\Book', 'bk', 'with', 'bk.author=au.id');
          $query_data =$query->getQuery();
          $data = $query_data->getResult();
      

      遇到任何问题请告诉我

      建议 - 尝试自己编写代码。如果您遇到问题,请尝试调试。如果你不能,那么只寻求你尝试过的任何帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-25
        • 1970-01-01
        • 2013-08-14
        • 2012-07-14
        • 2012-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多