【问题标题】:Create a table for Phalcon model为 Phalcon 模型创建表
【发布时间】:2014-03-14 20:59:46
【问题描述】:

我是 Phalcon PHP 框架的初学者。

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

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 框架中的特定工具?

【问题讨论】:

    标签: php sql phalcon


    【解决方案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

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

    每个文件都包含一个独特的类,该类扩展了 Phalcon\Mvc\Model\Migration 这些类通常有两种方法: 上和下()。 Up() 执行迁移,而 down() 滚动它 返回。

    Up() 还包含魔法方法 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")
            );
        }
    }
    

    将生成的迁移上传到目标服务器后,您 可以很容易地运行它们,如下例所示:

    【讨论】:

    • 非常感谢!在哪里更好地找到此代码以自动创建表?
    • 您只需运行此代码一次,准备一个仅在初始设置时调用的文件
    猜你喜欢
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多