2014-12-03 63 views
0

我剛開始使用Phalcon框架。我從來沒有真正使用過一個框架(或者真的是MVC),所以這是一個學習曲線。使用Phalcon和關係插入數據

我已經創建了2個表格:UserClient

客戶端可以有很多User's,但用戶只能有1 Client

我有以下型號:

<?php 
class User extends \Phalcon\Mvc\Model 
{ 
    public $id; 
    public $name; 
    public $email; 
    public $username; 
    public $password; 
    public $active; 
    public $isAdmin; 
    public $client; 

    public function initialize() 
    { 
    $this->setConnectionService('gateway-db'); 
    $this->belongsTo("clientId", "Client", "id"); 
    } 
} 

<?php 
class Client extends \Phalcon\Mvc\Model 
{ 
    public $id; 
    public $code; 
    public $name; 
    public $active; 

    public function initialize() 
    { 
    $this->setConnectionService('gateway-db'); 
    $this->hasMany("id", "User", "clientId"); 
    } 
} 

我想創建一個鏈接到現有Client用下面的代碼的新User,但是ClientID的領域是NULL,而不是鏈接。

$client = Client::findFirstByCode("DEM"); 

$user = new User(); 
$user->email = "[email protected]"; 
$user->is_admin = 1; 
$user->username = "lock"; 
$user->active = 1; 
$user->name = "Lachlan"; 
$user->password = $this->security->hash("password"); 
$user->client = $client; 

我能做什麼錯?

回答

1

字段clintId不存在。您需要使用$this->belongsTo("id", "Client", "id");。客戶端模型也一樣。

注意:您的字段client可能是一個整數,因此它不能包含整個$client對象。

嘗試分配ID:$user->client = $client->id

也想想使用受保護的變量和getter/setter。