kohana - errorexception [خطأ فادح]: لا يمكن استخدام كائن نموذج النوع كصفيف
-
30-09-2019 - |
سؤال
هل يمكنك تقديم المشورة بشأن كيفية حل الخطأ التالي:
ErrorException [ Fatal Error ]: Cannot use object of type Model_Branch as array
يرجى الاطلاع على وحدة التحكم:
public function action_view($agent_id='') {
$agent = ORM::factory('agent', $agent_id);
if ($agent->loaded()) {
$values = $agent->as_array();
$branches = $agent->branches->find_all()->as_array();
// Show page
$this->template->title = $agent->company_name;
$this->template->content = View::factory('agent/view')
->bind('title', $this->template->title)
->bind('values', $values)
->bind('branches', $branches);
} else {
Request::instance()->redirect('agent');
}
}
المحلول
أنت لا تحتاج حقًا إلى as_array () هناك. تتصرف كائنات database_result كصفيف افتراضيًا ، يمكنك القيام به foreach ($branches as $b) echo $b->id
هناك حتى دون تحويله إلى صفيف ؛
Database_Result implements Countable, Iterator, SeekableIterator, ArrayAccess
سيكون الاستخدام الحالي الوحيد لـ database_result :: as_array () هو إنشاء مفتاح => صفيفات Val ، كما أشرت هنا. لا يمكنك حاليًا تحويل هذا إلى مجموعة من نتائج قاعدة البيانات ، على الرغم من أنه يبدو منطقيا في البداية.
نصائح أخرى
سأحاول هذا:
$branches = $agent->branches->find_all();
$branches = $branches->as_array();
قد ينجح ، في بعض الأحيان تحتاج إلى إعلانه قبل تحويله.
لا تنتمي إلى StackOverflow