Php 为Phalcon模型创建一个表
我是Phalcon PHP框架的初学者 我已经创建了一个模型。像这样:Php 为Phalcon模型创建一个表,php,sql,phalcon,Php,Sql,Phalcon,我是Phalcon PHP框架的初学者 我已经创建了一个模型。像这样: class User extends Phalcon\Mvc\Model { /** * @Primary * @Identity * @Column(type="integer", nullable=false) */ public $id; /** * @Column(type="string", nullable=false) */
class User extends Phalcon\Mvc\Model
{
/**
* @Primary
* @Identity
* @Column(type="integer", nullable=false)
*/
public $id;
/**
* @Column(type="string", nullable=false)
*/
public $username;
/**
* @Column(type="string", nullable=false)
*/
public $email;
/**
* @Column(type="string", nullable=false)
*/
public $first_name;
/**
* @Column(type="string", nullable=false)
*/
public $last_name;
/**
* @Column(type="string", nullable=false)
*/
public $password;
public function getSource()
{
return "users";
}
}
现在我想做一些ORM操作
User::count(array('email = :email:', 'email' => $email)) == 0;
我得到了这个错误:
SQLSTATE[42S02]:找不到基表或视图:1146表“dbname.users”不存在
该表实际上不存在,因此我应该如何创建它?手动通过SQL查询或使用Phalcon框架中的特定工具?在简要回顾了Phalcon文档之后,我想您正在寻找类似的东西=> 用法示例:
<?php
use \Phalcon\Db\Column as Column;
$connection->createTable(
"robots",
null,
array(
"columns" => array(
new Column("id",
array(
"type" => Column::TYPE_INTEGER,
"size" => 10,
"notNull" => true,
"autoIncrement" => true,
)
),
new Column("name",
array(
"type" => Column::TYPE_VARCHAR,
"size" => 70,
"notNull" => true,
)
),
new Column("year",
array(
"type" => Column::TYPE_INTEGER,
"size" => 11,
"notNull" => true,
)
)
)
)
);
非常感谢您!要自动创建表,最好在哪里找到此代码?您只需运行此代码一次,准备一个仅在初始设置时调用的文件
<?php
use Phalcon\Db\Column as Column;
use Phalcon\Db\Index as Index;
use Phalcon\Db\Reference as Reference;
class ProductsMigration_100 extends \Phalcon\Mvc\Model\Migration
{
public function up()
{
$this->morphTable(
"products",
array(
"columns" => array(
new Column(
"id",
array(
"type" => Column::TYPE_INTEGER,
"size" => 10,
"unsigned" => true,
"notNull" => true,
"autoIncrement" => true,
"first" => true,
)
),
new Column(
"product_types_id",
array(
"type" => Column::TYPE_INTEGER,
"size" => 10,
"unsigned" => true,
"notNull" => true,
"after" => "id",
)
),
new Column(
"name",
array(
"type" => Column::TYPE_VARCHAR,
"size" => 70,
"notNull" => true,
"after" => "product_types_id",
)
),
new Column(
"price",
array(
"type" => Column::TYPE_DECIMAL,
"size" => 16,
"scale" => 2,
"notNull" => true,
"after" => "name",
)
),
),
"indexes" => array(
new Index(
"PRIMARY",
array("id")
),
new Index(
"product_types_id",
array("product_types_id")
)
),
"references" => array(
new Reference(
"products_ibfk_1",
array(
"referencedSchema" => "invo",
"referencedTable" => "product_types",
"columns" => array("product_types_id"),
"referencedColumns" => array("id"),
)
)
),
"options" => array(
"TABLE_TYPE" => "BASE TABLE",
"ENGINE" => "InnoDB",
"TABLE_COLLATION" => "utf8_general_ci",
)
)
);
// insert some products
self::$_connection->insert(
"products",
array("Malabar spinach", 14.50),
array("name", "price")
);
}
}