【发布时间】: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