Reputation: 5522
I have just started to look at using the Phalcon framework. I've never really used a framework before (or really MVC for that matter) so it's a bit of a learning curve.
I have created 2 tables: User
and Client
.
A client can have many User
's, however a user can only have 1 Client
.
I have the following Models:
<?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");
}
}
I am trying to create a new User
that is linked to an existing Client
with the following code, however the clientId field is NULL
and not linked.
$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;
What could I be doing wrong?
Upvotes: 0
Views: 238
Reputation: 135
Field clintId is not present. You need to use $this->belongsTo("id", "Client", "id");
. Same goes for the Client Model.
Note: Your field client
is probably a integer, so it can't contain the whole $client
object.
Try to assign the id: $user->client = $client->id
.
Also think about using protected vars and getter/setter.
Upvotes: 1