/** * Handles constructing the relatedBeansAndModels with special attention to the case where it is PolyOneToMany * @param string $modelClassName * @param mixed $sqlOrBean */ private function constructRelatedBeansAndModels($modelClassName, $sqlOrBean = '') { assert('is_string($sqlOrBean) || $sqlOrBean instanceof RedBean_OODBBean'); $tableName = RedBeanModel::getTableName($modelClassName); if (is_string($sqlOrBean)) { $this->relatedBeansAndModels = array_values($beans = R::find($tableName, $sqlOrBean)); } else { assert('$sqlOrBean instanceof RedBean_OODBBean'); $this->bean = $sqlOrBean; try { if ($this->bean->id > 0) { if ($this->polyName != null) { $value = array(); $values['id'] = $this->bean->id; $values['type'] = $this->bean->getMeta('type'); $this->relatedBeansAndModels = array_values(R::find($tableName, strtolower($this->polyName) . '_id = :id AND ' . strtolower($this->polyName) . '_type = :type', $values)); } else { $relatedIds = ZurmoRedBeanLinkManager::getKeys($this->bean, $tableName); $this->relatedBeansAndModels = array_values(R::batch($tableName, $relatedIds)); } } else { $this->relatedBeansAndModels = array(); } } catch (RedBean_Exception_SQL $e) { // SQLSTATE[42S02]: Base table or view not found... // SQLSTATE[42S22]: Column not found... if (!in_array($e->getSQLState(), array('42S02', '42S22'))) { throw $e; } $this->relatedBeansAndModels = array(); } } }
/** * Constructs a new RedBeanModels which is a collection of classes extending model. * The models are created lazily. * Models are only constructed with beans by the model. Beans are * never used by the application directly. * The application can construct a new models object by constructing * them without specifying a bean. In other words, if Php had overloading * and friends the constructor without the $bean would be public, and the * constructor taking a bean would private and available only to RedBeanModel. */ public function __construct($modelClassName, $sqlOrBean = '') { assert('is_string($sqlOrBean) || $sqlOrBean instanceof RedBean_OODBBean'); $this->modelClassName = $modelClassName; $this->position = 0; $tableName = RedBeanModel::getTableName($modelClassName); if (is_string($sqlOrBean)) { $this->relatedBeansAndModels = array_values(R::find($tableName, $sqlOrBean)); } else { assert('$sqlOrBean instanceof RedBean_OODBBean'); $this->bean = $sqlOrBean; // I had this... // $this->relatedBeansAndModels = array_values(R::related($this->bean, $tableName)); // and the doco says to use related in place of findLinks which is deprecated, // but that is returning zero when I know there are linked beans. try { // So I'm getting them with the link manager, but that can // throw if the table or column doesn't exist yet because there // are no linked beans. if ($this->bean->id > 0) { $relatedIds = ZurmoRedBeanLinkManager::getKeys($this->bean, $tableName); $this->relatedBeansAndModels = array_values(R::batch($tableName, $relatedIds)); } else { $this->relatedBeansAndModels = array(); } } catch (RedBean_Exception_SQL $e) { // SQLSTATE[42S02]: Base table or view not found... // SQLSTATE[42S22]: Column not found... if (!in_array($e->getSQLState(), array('42S02', '42S22'))) { throw $e; } // If there is nothing yet linked // just have no related models yet. $this->relatedBeansAndModels = array(); } } }