sevenways / zor

此包已被废弃且不再维护。未建议替代包。

Zend Framework 2 模块

dev-master 2017-06-13 17:58 UTC

This package is not auto-updated.

Last update: 2024-01-06 16:13:17 UTC


README

            Zend On Rails by Sergej Hoffmann 0.9.1beta

此模块实现了 Ruby On Rails 的一些功能,例如 ActiveRecord 模式和迁移。

                    ZendOnRails Instruction

安装所需程序。

  sudo apt-get install php7.0 php7.0-zip php7.0-intl php7.0-xml php7.0-sqlite
  
  sudo apt-get install composer
  
  sudo apt-get install git

安装包。

Create Project Folder.
        mkdir <project_folder>
Navigate to the project folder.
        cd <project_folder>
Install the required packages with "Composer".

        composer require sevenways / zor: dev-master

创建

        alias ​​zor = 'vendor/sevenways/zor/bin/zor.php'

或使用

    ./vendor/bin/zor.php


                  Working with ZendOnRails module
  zor.php create project [--path=]    Create an application. It uses  ZendApplicatioSkeleton
       
  [--path]    Optional if workspace differently

  zor.php create module --name= [--path=]    Create a module. It uses ZendModuleSkeleton

  [--name]    Name of Module
  [--path]    Optional if workspace differently

  zor.php create fmodule --require= [--path=]    Create a foreign module from packagist.org

  [--require]    Package name from packagist.org
  [--path]       Optional if workspace differently

  zor.php create database [--name=] [--driver=] [--username=] [--password=]    Create a database adapter. Default Sqlite

  [--name]        Name of tabel
  [--driver]      Zend Farmework supports drivers
  [--username]    Database username
  [--password]    Database password

  zor.php generate ctrl --name= [--module=] [--actions=]    Generate a controller

  [--name]       Name of controller
  [--module]     Name of module. Default: "Application"
  [--actions]    Names of actions. Default: "index"

  zor.php generate act [--cname=] [--module=] [--actions=]    Generate the actions for a controller

  [--cname]      Name of controller
  [--module]     Name of module. Default:"Application"
  [--actions]    Names of actions. Default: "index"

  zor.php generate model [--name=] [--module=] [--columns=]    Generate a model with ActiveRecord pattern

  [--name]       Name of model
  [--module]     Name of module. Default:"Application"
  [--columns]    A string of attributs.

  Structure of the string for the Columns: first_name:type{length}:primerykey/uniquekey,next_column:type{length}:Primerykey/uniquekey,...

  zor.php generate migration [--name=] [--columns=]    Generate a migration

  [--name]       Name of migration
  [--columns]    A string of attributs

  Structure of the string for the Columns: 

    first_name:type{length}:primerykey/uniquekey,next_column:type{length}:Primerykey/uniquekey,...


  zor.php run server [--host=] [--port=] [--path=]    Run buildin PHP server

  [--host]    Name of migration. Default: "localhost"
  [--port]    Port nummber. Default: "8080"
  [--path]    Path to index.php. Default: "/public"

  zor.php db migrate [--version]     Run migration to database
  zor.php db rollback [--version]    Run rollback to database

  [--version]    Version of migration. Default: any
           Working with ActiveRecord

创建数据库/模型。ZOR 通过以下命令生成模型和迁移

  zor.php generate model [--name =] [--module=] [--columns=]

属性 id、created_at 和 updated_at 将自动生成。创建模型有两种方式

第一种方法:生成器将每个生成的模型绑定到 Service Manager。这允许我们通过以下调用从控制器中创建访问

$obj = $this->serviceLocator->get('Namespace\Model\ModelName');

因为每个 ActiveRecord 对象都实现了 AdapterAwareInterface,数据库适配器将由 ServiceManager 自动添加。

第二种方法:通过正常的对象生成,你必须将数据库适配器传递给创建的对象。

$obj = new ModelName();
$obj-> setDbAdapter($adapterObject);
            Add and modify records

记录可以通过两种方式添加。

$obj->create(array (field_name1 => value, field_name2 => value, ...));
$obj->bind(array (field_name1 => value, field_name2 => value, ...));

其中 bind() 方法是执行 save() 方法。

$obj->update_attributes(array (field_name1 => 'value', field_name2 => 'value', ...));
$obj->columnName = 'value';

对于值的变化,必须执行 save() 方法。

$obj->save();

有用方法

$obj->isNewRecord();   // Verifies if Model is stored in the database.

$obj->isChanged();     // Returns whether a column value has been changed
        Working with individual entries
first($like=1) - returns the first entry from the database
last($like=1) - returns the last entry from the database
all() - returns all entries from the database
take($like) - is synonymous with first() where argument $like is required
find($id) - searches for one or more entries in the database by attributes(PrimaryKey). For multiple entries, you must pass ID as array().
find_by_attribute($name, $argument) - looks for an entry using the attribute name and value.

find_or_create_by_attribute ($column, $argument, $_) - this function checks if an entry exists, otherwise it inserts new.

使用 $_ 变量可以传递一个包含关联属性及其值的数组。

有魔法方法

find_by_* ($value);
find_or_create_by_* ($value, $_=null);

而不是星号,设置属性的名称。

        Working with relationship 1:N

必须在两个模型中进行设置

在 Model_1 中

protected $ has_many = array('model_N' => array('class' => 'Namespace\ModelName');

在 Model_N 中

protected $ belongs_to = array('model_1' => array ('class' => 'Namespace\ModelName', 'foreign_key_attribute' => 'model_name_id', 'foreign_key' => null);

$nModel = $ obj->NameOfNModel(); //gets new N-model

$obj->NameOfNModel()->create(array(field_name => value));
$all_n_mdele = $obj->NameOfNModel()->all(); // returns an array of N-relationship models.
        Working with relationship M:N

M:N 关系需要第三个表,我们可以通过生成器创建一个表。

生成模型 Model_B[--module =] --columns=Model_M:references, Model_N:references

在 Model_B 中

protected $ has_many = array('Model_M' => array ('class' => 'Namespace\ModelName_M'),
                             'Model_N' => array('class' => 'Namespace\ModelName_N'));

在 Model_M 中

protected $ has_many = array ('Model_N' => array('class' => 'Namespace\ModelName','through' => 'Model_B');

在 Model_N 中

protected $ belongs_to = array ('Model_M' => array ('class' => 'namespace\ModelName', 'foreign_key_attribute' => 'model_name_id', 'foreign_key' => null);