我正在尝试编写一个类,允许您在基于php MVC的项目中建立两个模型之间的关系。我想创建一个只包含你想要的字段的自定义对象所以我写了这个:
class CreateRelationShipObjects {
private $modelObjects;
public function __construct($modelObjects){
if(is_array($modelObjects) && isset($modelObjects)){
$this->modelObjects = $modelObjects;
}
}
public function createRelationShip($columns){
if(is_array($columns)){
foreach($columns as $custom_name=>$column){
foreach($this->modelObjects as $name => $modelObject){
$this->checkForColumnInModelObject($modelObject, $column, $custom_name);
}
}
}
}
private function checkForColumnInModelObject($modelObject, $column, $custom_name){
$relationship = array();
if(property_exists($modelObject, $column)){
$value_returned = $modelObject->$column;
$relationship = array($column => $value_returned);
var_dump($relationship);
}
//return $this->toObj($relationship);
}
private function toObj($releationshipArray){
//var_dump((Object)$releationshipArray);
}
}
这个想法就是你这样使用这个类:
$relationship = new CreateRelationShipObjects(array(
'equipment_ip_models' => $equipmentIpModels,
'site_models' => $siteModel,
'equipment_models' => $equipmentModel
));
$relationship->createRelationShip(array(
'equipment_model' => 'model',
'equipment_manufacture' => 'manufacture',
'equipment_ip' => 'ipAddress',
'site_name' => 'name',
'site_id' => 'code'
));
在上面的课程中,我们将查看为该列提供的每个模型。然后返回一个看起来与createRelationShip
问题是,我回来了,我的var_dump
,这样的数组:
array(1) {
["model"]=>
string(13) "4168.21.33.03"
}
array(1) {
["manufacture"]=>
string(6) "ALLGON"
}
// ... And so on
数组应如下所示:
array(
'equipment_model' => '4168.21.33.03',
'equipment_manufacture' => 'ALLGON',
// .. and so on ...
);
有人可以告诉我我做错了吗?
答案 0 :(得分:0)
也许这效果更好?如果修改这两个函数?
public function createRelationShip($columns){
if(is_array($columns)){
$relationship = array();
foreach($columns as $custom_name=>$column){
foreach($this->modelObjects as $name => $modelObject){
$relationship += $this->checkForColumnInModelObject($modelObject, $column, $custom_name);
}
}
var_dump($relationship);
}
}
private function checkForColumnInModelObject($modelObject, $column, $custom_name){
if(property_exists($modelObject, $column)){
$value_returned = $modelObject->$column;
return array($column => $value_returned);
}
return array();
}
答案 1 :(得分:0)
下面的课程基本上就是我需要做的。它花了我一整夜,但我写了它。它接受了我传递给我的东西并给了我一个像我想要的对象。
class RelationshipObject {
/**
* Stores the models
*
* @var type
*/
private $modelObjects;
/**
* Takes in the objects
*
* @param array $objects
*/
public function __construct($objects) {
$this->modelObjects = $objects;
}
/**
* Creates a custom object based on the array passed in.
*
* @param array $outputKeysToInputKeys
* @return obj
*/
public function create(array $outputKeysToInputKeys) {
$result = array();
foreach ($outputKeysToInputKeys as $outputKey => $inputKey) {
foreach ($this->modelObjects as $object) {
if (property_exists($object, $inputKey))
$result[$outputKey] = $object->$inputKey;
}
}
return $this->to_obj($result);
}
/**
* Converts an array to an object.
*
* @param array $array
* @return obj
*/
public function to_obj($array){
return (Object)$array;
}
}