我想知道为什么我无法在我的/ hospital / index中看到他们所属的群组。我正在关注这篇文章http://li3.me/docs/manual/models/relationships.md
但是,我没有看到包含在关联组中的关联数组。 下面是我的视图模型和控制器
//hopsitals model
namespace app\models;
class Hospitals extends \lithium\data\Model {
public $belongsTO = array('Groups');
}
这里是群组模型
//groups model
namespace app\models;
class Groups extends \lithium\data\Model {
public $hasMany = array('Hospitals');
}
然后我的医院控制器看起来像这样
//hospitals controller
namespace app\controllers;
use app\models\hospitals;
use app\models\groups;
class HospitalsController extends \lithium\action\Controller {
public function index() {
$hospitals = Hospitals::find('all', array(
'with' => 'Groups'
));
$hospitals = $hospitals->to('array');
// $hospitals = $hospitals->to('json');
return compact('hospitals');
}
public function add() {
if($this->request->data) {
Hospitals::create($this->request->data)->save();
}
}
$groups = Groups::find('all');
$groupsList = array();
foreach($groups as $group) {
$group = $group->to('array');
$groupList[$group['_id']] = $group['group_name'];
}
$this->set(compact('groupList'));
}
}
这里是群组控制器。
namespace app\controllers;
use app\models\groups;
class GroupsController extends \lithium\action\Controller
{
public function add()
{
$group = Groups::create();
$success = false;
if ($this->request->data && $group->save($this->request->data)) {
$success = true;
}
return compact('group', 'success');
}
public function index(){
$groups = Groups::find('all', array(
'with' => 'Hospitals'
));
$groups = $groups->to('array');
return compact('groups');
}
}
所以一个小组可以拥有多家医院。但是我没有在var_dumps中看到医院属于哪些组(如果有的话),或者组中是否有任何医院。
任何帮助将不胜感激。
这里是mongo db
{ "_id" : ObjectId("54bd8a198dcfba8a29000001"), "group_id" : "54bd7e448dcfba5229000000", "hospital_name" : "test3", "hospital_subtitle" : "test3", "hospital_add_1" : "test3", "hospital_add_2" : "test3", "hospital_city" : "test3", "hospital_state" : "test3", "hospital_zip" : "test3", "hospital_phone" : "test3", "hospital_fax" : "test3", "hospital_description" : "test3", "callcenter_agent_approval" : "test3", "hospital_site" : "test3", "mdpocket_approval" : "test3", "hospital_active" : "test3", "facebook" : "test3" }
> db.groupss.find()
> db.groups.find()
{ "_id" : ObjectId("54bd7e448dcfba5229000000"), "group_name" : "group 123", "group_num" : "00001", "group_desc" : "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.", "group_logo" : "n/a", "group_address1" : "40 abourne road", "group_address2" : "n/a", "group_city" : "NM", "group_state" : "NM", "group_zip" : "00022", "group_website" : "test.com", "group_facebook" : "facebook.com/groups", "group_phone" : "212344455", "group_fax" : "345345345434", "group_contact_name" : "Asim", "group_subs_status" : "Active", "group_subs_start_date" : "2014-11-17", "created" : "n/a", "modified" : "n/a" }
>
我在群组_relations上做了一个var_dump,我得到了
array (size=1)
'Hospitals' =>
object(lithium\data\model\Relationship)[34]
protected '_classes' =>
array (size=1)
'entity' => string 'lithium\data\Entity' (length=19)
protected '_config' =>
array (size=10)
'link' => string 'embedded' (length=8)
'fieldName' => string 'hospitals' (length=9)
'name' => string 'Hospitals' (length=9)
'type' => string 'hasMany' (length=7)
'from' => string 'app\models\Groups' (length=17)
'key' =>
array (size=0)
...
'to' => string 'app\models\Hospitals' (length=20)
'fields' => boolean true
'constraints' =>
array (size=0)
...
'init' => boolean true
protected '_autoConfig' =>
array (size=0)
empty
protected '_methodFilters' =>
array (size=0)
empty
感谢