「Php/Symfony/Symfony2/doctrine/基本」の版間の差分
ナビゲーションに移動
検索に移動
| 62行目: | 62行目: | ||
* Get id | * Get id | ||
* | * | ||
* @return integer | * @return integer | ||
*/ | */ | ||
public function getId() | public function getId() | ||
| 73行目: | 73行目: | ||
* | * | ||
* @param string $name | * @param string $name | ||
* @return | * @return User | ||
*/ | */ | ||
public function setName($name) | public function setName($name) | ||
| 83行目: | 83行目: | ||
* Get name | * Get name | ||
* | * | ||
* @return string | * @return string | ||
*/ | */ | ||
public function getName() | public function getName() | ||
| 94行目: | 94行目: | ||
* | * | ||
* @param string $description | * @param string $description | ||
* @return | * @return User | ||
*/ | */ | ||
public function setDescription($description) | public function setDescription($description) | ||
2016年12月22日 (木) 05:55時点における版
db設定
app/config/parameters.ini parameters database_driver = pdo_mysql database_host = localhost database_name = project1 database_user = user1 database_password = *****
dbのschemaを作る
php app/console doctrine:database:create
or
create database project1;
テーブルクラス作成
-Acme/HelloBundle/Entity/User.php
namespace Acme\HelloBundle\Entity;
class User
{
protected $name;
protected $description;
}
generateでテーブルクラスを作った場合
dbとのマッピング処理などが追加された記述をして作成される
$ php app/console doctrine:generate:entity --entity="AcmeHelloBundle:User" --fields="name:string(255) description:text"
-Acme/HelloBundle/Entity/User.php
namespace Acme\HelloBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @ORM\Table()
* @ORM\Entity
*/
class User
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return User
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
* @return User
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string-
*/
public function getDescription()
{
return $this->description;
}
}
generateしたテーブルクラスからdb側のtableを作る
$ php app/console doctrine:schema:update --force CREATE TABLE `User2` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `description` longtext COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
insert処理
$user = new \Acme\HelloBundle\Entity\User();
$user->setName('taro');
$user->setDescription('太郎です');
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->flush(); // ここでinsert処理がされる
select * from User; +----+-----------+-------------------+ | id | name | description | +----+-----------+-------------------+ | 1 | taro | 太郎です | +----+-----------+-------------------+
select処理
$repository = $this->getDoctrine()
->getRepository('AcmeHelloBundle:User');
$user = $repository->find(1); // userクラスにデータを入れて取得
if ($user) {
echo $user->getId(); // taro
echo $user->getName(); // 太郎です
}