Reputation: 3255
I am a beginner in Phalcon PHP Framework.
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.
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
The table is really not exists, so how I should create it? Manually via SQL Query or using a specific tool in Phalcon Framework?
Upvotes: 2
Views: 8626
Reputation: 9323
After a brief review of the phalcon docs i suppose that you are looking for something like this => Phalcon PHP creating tables
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. 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(). Up() performs the migration, while down() rolls it back.
Up() also contains the magic method 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:
Upvotes: 1