为Phalcon模型创建一个表

时间:2014-02-15 12:08:20

标签: php sql phalcon

我是Phalcon PHP Framework的初学者。

我创建了一个模型。像这样:

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]: Base table or view not found: 1146 Table 'dbname.users' doesn't exist

该表确实不存在,所以我应该如何创建它?手动通过SQL查询或使用Phalcon Framework中的特定工具?

1 个答案:

答案 0 :(得分:1)

在对phalcon文档进行简要回顾之后,我想你正在寻找像这样的东西=> Phalcon PHP creating tables

使用样本:

<?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,
                )
            )
        )
    )
);

如何设置迁移:Source

enter image description here

  

默认情况下,Phalcon Developer Tools使用app / migrations目录   转储迁移文件。您可以通过设置一个来更改位置   生成脚本上的参数。数据库中的每个表   将其各自的类生成在a下的单独文件中   目录引用其版本:

enter image description here

  

每个文件都包含一个扩展的唯一类   Phalcon \ Mvc \ Model \ Migration这些类通常有两种方法:   上和下()。 Up()执行迁移,而down()则执行迁移   回来。

     

Up()还包含magic方法morphTable()。魔术来了   它识别同步实际表所需的更改   数据库给出的描述。

<?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")
        );
    }
}
  

在目标服务器上上传生成的迁移后,即可   可以轻松运行它们,如以下示例所示:

enter image description here