简体   繁体   English

为Phalcon模型创建表

[英]Create a table for Phalcon model

I am a beginner in Phalcon PHP Framework. 我是Phalcon PHP框架的初学者。

I've created a model. 我已经创建了一个模型。 Like this: 像这样:

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

So now I want to do some ORM operations. 所以现在我想做一些ORM操作。

User::count(array('email = :email:', 'email' => $email)) == 0;

And I getting this error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dbname.users' doesn't exist 我收到此错误: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dbname.users' doesn't exist

The table is really not exists, so how I should create it? 该表确实不存在,那么我应该如何创建它呢? Manually via SQL Query or using a specific tool in Phalcon Framework? 通过SQL查询手动还是使用Phalcon Framework中的特定工具?

After a brief review of the phalcon docs i suppose that you are looking for something like this => Phalcon PHP creating tables 在简要回顾了Phalcon文档之后,我想您正在寻找类似这样的东西=> Phalcon PHP创建表

Sample of usage: 用法样本:

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

Howto setup a migration : Source 如何设置迁移:

在此处输入图片说明

By default Phalcon Developer Tools use the app/migrations directory to dump the migration files. 默认情况下,Phalcon开发人员工具使用app / migrations目录转储迁移文件。 You can change the location by setting one of the parameters on the generation script. 您可以通过在生成脚本上设置参数之一来更改位置。 Each table in the database has its respective class generated in a separated file under a directory referring its version: 数据库中的每个表都有各自的类,这些类在引用其版本的目录下的单独文件中生成:

在此处输入图片说明

Each file contains a unique class that extends the Phalcon\\Mvc\\Model\\Migration These classes normally have two methods: up() and down(). 每个文件都包含一个扩展Phalcon \\ Mvc \\ Model \\ Migration的唯一类。这些类通常具有两种方法:up()和down()。 Up() performs the migration, while down() rolls it back. Up()执行迁移,而down()将其回滚。

Up() also contains the magic method morphTable(). Up()还包含魔术方法morphTable()。 The magic comes when it recognizes the changes needed to synchronize the actual table in the database to the description given. 当魔术识别出将数据库中的实际表与给定描述同步所需的更改时,魔术就来了。

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

Once the generated migrations are uploaded on the target server, you can easily run them as shown in the following example: 将生成的迁移上传到目标服务器后,您可以轻松地运行它们,如以下示例所示:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM