-
Notifications
You must be signed in to change notification settings - Fork 39
Db Row
AntonShevchuk edited this page Feb 20, 2013
·
29 revisions
Строка в таблице, аналогично принятому в ZFCore
Для корректной работы требует класс соответствующий класс Table
<?php
namespace Application\Users;
class Row extends \Bluz\Db\Row
{
public function preInsert()
{
$this->created = gmdate('Y-m-d H:i:s');
}
public function preUpdate()
{
$this->updated = gmdate('Y-m-d H:i:s');
}
}
$userRow = new \Application\Users\Row();
$userRow -> login = 'username';
$userRow -> save();
Можно вытащить связанный записи следующим образом:
<?php
$userRows = $db->fetchObjects('
select
users.*
, g.name AS __groups_name
from users
left join groups g on users.group_id = g.id
', array(), '\Application\Users\Row');
foreach($userRows as $userRow)
$groupRow = $userRow->getRelation('Groups');
Обратите внимание на SELECT, в нём можно увидеть конструкцию - g.name AS __groups_name, тут groups - имя связанного объекта Row и его атрибут.
- переименовать preInsert -> beforeCreate postInsert -> afterCreate ... и т.д
- добавить валидацию на уровне модели (а не Crud)
- организация взаимосвязей подобным образом мне не нравится
| users | | posts | | posts_tags | | tags |
| id | | id | | postId | | id |
| name | | title | | tagId | | name |
| userId |
<?php
namespace Application\Users;
class Row extends \Bluz\Db\Row {
public $id;
public $name;
}
class Table extends \Bluz\Db\Table {
protected $table = 'users'; // can retrieve from NS
protected $primary = ['id']; // can retrieve from DB info
}
namespace Application\Posts;
class Row extends \Bluz\Db\Row {
public $id;
public $title;
public $userId;
}
class Table extends \Bluz\Db\Table {
/* manyToOne */
public function init()
{
$this->manyToOne('userId', 'Users', 'id');
$this->manyToMany('id', 'PostsTags', 'postId', 'Tags');
}
}
namespace Application\PostsTags;
class Row extends \Bluz\Db\Row {
public $postId;
public $tagId;
}
class Table extends \Bluz\Db\Table {
/* manyToOne */
public function init()
{
$this->manyToOne('postId', 'Posts', 'id');
$this->manyToOne('tagId', 'Tags', 'id');
}
}
namespace Application\Tags;
class Row extends \Bluz\Db\Row {
public $id;
public $name;
}
class Table extends \Bluz\Db\Table {
/* manyToOne */
public function init()
{
$this->manyToMany('id', 'PostsTags', 'tagId', 'Posts');
}
}
Acl
Application
Auth
Cache
Common
— Exception
— Collection
— Container
— Helper
— Options
— Singleton
Config
Controller
— Data
— Mapper
—— Crud
—— Rest
— Reflection
Crud
— Crud Table
Db
— Row
— Table
— Relations
— Query
Debug
EventManager
Grid
Http
Layout
Logger
Mailer
Messages
Nil
Proxy
Registry
Request
Response
Router
Session
Translator
Validator
View