public function getPhoto() { $c = new Criteria(); $c->add(ElementFilePeer::FILE_TYPE, 1); $c->add(ElementFilePeer::ELEMENT_ID, $this->getId()); $photo = ElementFilePeer::doSelectOne($c); if ($photo) { return $photo->getName(); } return false; }
protected function processForm(sfWebRequest $request, sfForm $form) { $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); if ($form->isValid()) { $notice = $form->getObject()->isNew() ? 'Элемент создан успешно.' : 'Элемент изменен успешно.'; $form->getObject()->setCompanyId($request->getParameter('element_company')); $form->getObject()->setPriceType($request->getParameter('element_price_type')); if ($form->getObject()->isNew()) { $form->getObject()->setDateCreated(time()); } $form->getObject()->setDateUpdated(time()); $form->getObject()->setCategoryId($request->getParameter('element_category_id')); $Element = $form->save(); foreach ((array) $request->getParameter('element_file') as $key => $value) { if (isset($value['delete'])) { $file_delete = ElementFilePeer::retrieveByPk($key); $file_delete->delete(); } } foreach ((array) $request->getFiles('element_file') as $key => $value) { list($type, $ext) = explode('/', $value['type']); if (isset($value['id']) and $value['id'] != 0) { $file = ElementFilePeer::retrieveByPk($value['id']); } else { $file = new ElementFile(); } $file->setElementId($Element->getId()); $file->setName(md5(time()) . '.' . $ext); $file->setFileType($type == 'image' ? 1 : 2); $file->save(null, $value); } foreach ((array) $request->getParameter('element_preference') as $key => $value) { if (isset($value['id']) and $value['id'] != 0) { $preference = PreferencePeer::retrieveByPk($value['id']); } else { $preference = new Preference(); } $preference->setElementId($Element->getId()); $preference->setCategoryPreferenceId($value['category_preference_id']); $preference->setValue(isset($value['value']) ? $value['value'] : 0); $preference->save(); } $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $Element))); if ($request->hasParameter('_save_and_add')) { $this->getUser()->setFlash('notice', $notice . ' Можно добавить еще один.'); $this->redirect('@element_new'); } else { $this->getUser()->setFlash('notice', $notice); $this->redirect(array('sf_route' => 'element_edit', 'sf_subject' => $Element)); } } else { $this->getUser()->setFlash('error', 'Эелемент не сохранен, так как некоторые поля заполненны не верно!', false); } }
/** * Returns the number of related ElementFile objects. * * @param Criteria $criteria * @param boolean $distinct * @param PropelPDO $con * @return int Count of related ElementFile objects. * @throws PropelException */ public function countElementFiles(Criteria $criteria = null, $distinct = false, PropelPDO $con = null) { if ($criteria === null) { $criteria = new Criteria(ElementPeer::DATABASE_NAME); } else { $criteria = clone $criteria; } if ($distinct) { $criteria->setDistinct(); } $count = null; if ($this->collElementFiles === null) { if ($this->isNew()) { $count = 0; } else { $criteria->add(ElementFilePeer::ELEMENT_ID, $this->id); $count = ElementFilePeer::doCount($criteria, false, $con); } } else { // criteria has no effect for a new object if (!$this->isNew()) { // the following code is to determine if a new query is // called for. If the criteria is the same as the last // one, just return count of the collection. $criteria->add(ElementFilePeer::ELEMENT_ID, $this->id); if (!isset($this->lastElementFileCriteria) || !$this->lastElementFileCriteria->equals($criteria)) { $count = ElementFilePeer::doCount($criteria, false, $con); } else { $count = count($this->collElementFiles); } } else { $count = count($this->collElementFiles); } } return $count; }
/** * This is a method for emulating ON DELETE CASCADE for DBs that don't support this * feature (like MySQL or SQLite). * * This method is not very speedy because it must perform a query first to get * the implicated records and then perform the deletes by calling those Peer classes. * * This method should be used within a transaction if possible. * * @param Criteria $criteria * @param PropelPDO $con * @return int The number of affected rows (if supported by underlying database driver). */ protected static function doOnDeleteCascade(Criteria $criteria, PropelPDO $con) { // initialize var to track total num of affected rows $affectedRows = 0; // first find the objects that are implicated by the $criteria $objects = ElementPeer::doSelect($criteria, $con); foreach ($objects as $obj) { // delete related Preference objects $criteria = new Criteria(PreferencePeer::DATABASE_NAME); $criteria->add(PreferencePeer::ELEMENT_ID, $obj->getId()); $affectedRows += PreferencePeer::doDelete($criteria, $con); // delete related Order objects $criteria = new Criteria(OrderPeer::DATABASE_NAME); $criteria->add(OrderPeer::ELEMENT_ID, $obj->getId()); $affectedRows += OrderPeer::doDelete($criteria, $con); // delete related ElementFile objects $criteria = new Criteria(ElementFilePeer::DATABASE_NAME); $criteria->add(ElementFilePeer::ELEMENT_ID, $obj->getId()); $affectedRows += ElementFilePeer::doDelete($criteria, $con); } return $affectedRows; }
/** * Populates the object using an array. * * This is particularly useful when populating an object from one of the * request arrays (e.g. $_POST). This method goes through the column * names, checking to see whether a matching key exists in populated * array. If so the setByName() method is called for that column. * * You can specify the key type of the array by additionally passing one * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME, * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM. * The default key type is the column's phpname (e.g. 'AuthorId') * * @param array $arr An array to populate the object from. * @param string $keyType The type of keys the array uses. * @return void */ public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME) { $keys = ElementFilePeer::getFieldNames($keyType); if (array_key_exists($keys[0], $arr)) { $this->setId($arr[$keys[0]]); } if (array_key_exists($keys[1], $arr)) { $this->setName($arr[$keys[1]]); } if (array_key_exists($keys[2], $arr)) { $this->setFileType($arr[$keys[2]]); } if (array_key_exists($keys[3], $arr)) { $this->setElementId($arr[$keys[3]]); } }
/** * Retrieve multiple objects by pkey. * * @param array $pks List of primary keys * @param PropelPDO $con the connection to use * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function retrieveByPKs($pks, PropelPDO $con = null) { if ($con === null) { $con = Propel::getConnection(ElementFilePeer::DATABASE_NAME, Propel::CONNECTION_READ); } $objs = null; if (empty($pks)) { $objs = array(); } else { $criteria = new Criteria(ElementFilePeer::DATABASE_NAME); $criteria->add(ElementFilePeer::ID, $pks, Criteria::IN); $objs = ElementFilePeer::doSelect($criteria, $con); } return $objs; }