示例#1
0
 /**
  * Insert a new entity
  *
  * This function will either clone a entity prototype or create a new instance of the entity object for each
  * entity being inserted. By default the entity will be cloned. The entity will be stored by it's identity_key
  * if set or otherwise by it's object handle.
  *
  * @param   ModelEntityInterface|array $entity  A ModelEntityInterface object or an array of entity properties
  * @param   string  $status     The entity status
  * @return  ModelEntityComposite
  */
 public function insert($entity, $status = null)
 {
     if (!$entity instanceof ModelEntityInterface) {
         if (!is_array($entity) && !$entity instanceof \Traversable) {
             throw new \InvalidArgumentException('Entity must be an array or an object implementing the Traversable interface; received "%s"', gettype($entity));
         }
         if ($this->_prototypable) {
             if (!$this->_prototype instanceof ModelEntityInterface) {
                 $identifier = $this->getIdentifier()->toArray();
                 $identifier['path'] = array('model', 'entity');
                 $identifier['name'] = StringInflector::singularize($this->getIdentifier()->name);
                 //The entity default options
                 $options = array('identity_key' => $this->getIdentityKey());
                 $this->_prototype = $this->getObject($identifier, $options);
             }
             $prototype = clone $this->_prototype;
             $prototype->setStatus($status);
             $prototype->setProperties($entity, $prototype->isNew());
             $entity = $prototype;
         } else {
             $identifier = $this->getIdentifier()->toArray();
             $identifier['path'] = array('model', 'entity');
             $identifier['name'] = StringInflector::singularize($this->getIdentifier()->name);
             //The entity default options
             $options = array('data' => $entity, 'status' => $status, 'identity_key' => $this->getIdentityKey());
             $entity = $this->getObject($identifier, $options);
         }
     }
     //Insert the entity into the collection
     $this->__entities->insert($entity);
     return $this;
 }
示例#2
0
 /**
  * Insert a new row
  *
  * This function will either clone the row prototype, or create a new instance of the row object for each row
  * being inserted. By default the prototype will be cloned. The row will be stored by it's identity_column if
  * set or otherwise by it's object handle.
  *
  * @param   DatabaseRowInterface|array $row  A DatabaseRowInterface object or an array of row properties
  * @param   string  $status     The row status
  * @return  DatabaseRowsetAbstract
  */
 public function insert($row, $status = null)
 {
     if (!$row instanceof DatabaseRowInterface) {
         if (!is_array($row) && !$row instanceof \Traversable) {
             throw new \InvalidArgumentException('Row must be an array or an object implementing the Traversable interface; received "%s"', gettype($row));
         }
         if ($this->_prototypable) {
             if (!$this->_prototype instanceof DatabaseRowInterface) {
                 $this->_prototype = $this->getTable()->createRow();
             }
             $prototype = clone $this->_prototype;
             $prototype->setStatus($status);
             $prototype->setProperties($row, $prototype->isNew());
             $row = $prototype;
         } else {
             $config = array('data' => $row, 'status' => $status);
             $row = $this->getTable()->createRow($config);
         }
     }
     //Insert the row into the rowset
     $this->__rowset->insert($row);
     return $this;
 }