Symofny3.0 Doctrine 操作
-
配置数据库链接
//app/config/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: 3306
database_name: symfonty
database_user: root
database_password: 123456
secret: ThisTokenIsNotSoSecretChangeIt
> 在命令行中执行下面命令测试是否连接成功(创建数据库和表)
> php bin/console doctrine:database:create
> php bin/console doctrine:schema:update --force
* 在AppBundle 新建Entity文件夹用来创建数据库的字段
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="wechatevent")
*/
class WechatEvent{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=100)
*/
private $toUserName;
/**
* @ORM\Column(type="string", length=100)
*/
private $fromUserName;
}
// 创建了3个字段id,toUserName, formUserName
* 为这3 个字段添加get set 属性,来操作增删查改
> $php bin/console doctrine:generate:entities AppBundle
* 在control创建路由测试
``` php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\WechatEvent;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction()
{
$msg = new WechatEvent();
$msg->setToUserName('nick');
$msg->setFromUserName('nickback');
$em = $this->getDoctrine()->getManager();
$em->persist($msg);
$em->flush();
return new Response("done");
}
}
```
网友评论