本文整理汇总了PHP中PropelQuery::from方法的典型用法代码示例。如果您正苦于以下问题:PHP PropelQuery::from方法的具体用法?PHP PropelQuery::from怎么用?PHP PropelQuery::from使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PropelQuery
的用法示例。
在下文中一共展示了PropelQuery::from方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
protected function setUp()
{
parent::setUp();
BookstoreDataPopulator::populate($this->con);
Propel::disableInstancePooling();
$this->books = PropelQuery::from('Book')->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)->find();
}
示例2: getChoices
/**
* Returns the choices associated to the model.
*
* @return array An array of choices
*/
public function getChoices()
{
$choices = array();
if (false !== $this->getOption('add_empty')) {
$choices[''] = true === $this->getOption('add_empty') ? '' : $this->getOption('add_empty');
}
$criteria = PropelQuery::from($this->getOption('model'));
if ($this->getOption('criteria')) {
$criteria->mergeWith($this->getOption('criteria'));
}
foreach ($this->getOption('query_methods') as $methodName => $methodParams) {
if (is_array($methodParams)) {
call_user_func_array(array($criteria, $methodName), $methodParams);
} else {
$criteria->{$methodParams}();
}
}
if ($order = $this->getOption('order_by')) {
$criteria->orderBy($order[0], $order[1]);
}
$objects = $criteria->find($this->getOption('connection'));
$methodKey = $this->getOption('key_method');
if (!method_exists($this->getOption('model'), $methodKey)) {
throw new RuntimeException(sprintf('Class "%s" must implement a "%s" method to be rendered in a "%s" widget', $this->getOption('model'), $methodKey, __CLASS__));
}
$methodValue = $this->getOption('method');
if (!method_exists($this->getOption('model'), $methodValue)) {
throw new RuntimeException(sprintf('Class "%s" must implement a "%s" method to be rendered in a "%s" widget', $this->getOption('model'), $methodValue, __CLASS__));
}
foreach ($objects as $object) {
$choices[$object->{$methodKey}()] = $object->{$methodValue}();
}
return $choices;
}
示例3: doClean
/**
* @see sfValidatorBase
*/
protected function doClean($value)
{
$criteria = PropelQuery::from($this->getOption('model'));
if ($this->getOption('criteria')) {
$criteria->mergeWith($this->getOption('criteria'));
}
foreach ($this->getOption('query_methods') as $method) {
$criteria->{$method}();
}
if ($this->getOption('multiple')) {
if (!is_array($value)) {
$value = array($value);
}
$count = count($value);
if ($this->hasOption('min') && $count < $this->getOption('min')) {
throw new sfValidatorError($this, 'min', array('count' => $count, 'min' => $this->getOption('min')));
}
if ($this->hasOption('max') && $count > $this->getOption('max')) {
throw new sfValidatorError($this, 'max', array('count' => $count, 'max' => $this->getOption('max')));
}
$criteria->addAnd($this->getColumn(), $value, Criteria::IN);
$dbcount = $criteria->count($this->getOption('connection'));
if ($dbcount != $count) {
throw new sfValidatorError($this, 'invalid', array('value' => $value));
}
} else {
$criteria->addAnd($this->getColumn(), $value);
$dbcount = $criteria->count($this->getOption('connection'));
if (0 === $dbcount) {
throw new sfValidatorError($this, 'invalid', array('value' => $value));
}
}
return $value;
}
示例4: testFilterById
/**
* testFilterById
*
* Various test for filterById functions
* Id's are autoincrement so we have to use a Select to get current ID's
*
*/
public function testFilterById()
{
// find by single id
$book = PropelQuery::from('Book b')->where('b.Title like ?', 'Don%')->orderBy('b.ISBN', 'desc')->findOne();
$c = BookQuery::create()->filterById($book->getId());
$book2 = $c->findOne();
$this->assertTrue($book2 instanceof Book);
$this->assertEquals('Don Juan', $book2->getTitle());
//find range
$booksAll = PropelQuery::from('Book b')->orderBy('b.ID', 'asc')->find();
$booksIn = BookQuery::create()->filterById(array($booksAll[1]->getId(), $booksAll[2]->getId()))->find();
$this->assertTrue($booksIn[0] == $booksAll[1]);
$this->assertTrue($booksIn[1] == $booksAll[2]);
// filter by min value with greater equal
$booksIn = null;
$booksIn = BookQuery::create()->filterById(array('min' => $booksAll[2]->getId()))->find();
$this->assertTrue($booksIn[1] == $booksAll[3]);
// filter by max value with less equal
$booksIn = null;
$booksIn = BookQuery::create()->filterById(array('max' => $booksAll[1]->getId()))->find();
$this->assertTrue($booksIn[1] == $booksAll[1]);
// check backwards compatibility:
// SELECT FROM `book` WHERE book.id IN (:p1,:p2)
// must be the same as
// SELECT FROM `book` WHERE (book.id>=:p1 AND book.id<=:p2)
$minMax = BookQuery::create()->filterById(array('min' => $booksAll[1]->getId(), 'max' => $booksAll[2]->getId()))->find();
$In = BookQuery::create()->filterById(array($booksAll[1]->getId(), $booksAll[2]->getId()))->find();
$this->assertTrue($minMax[0] === $In[0]);
$this->assertTrue(count($minMax->getData()) === count($In->getData()));
}
示例5: getSelection
/**
* @return \BaseObject|null
* @throws \PropelException
*/
public function getSelection()
{
$pk = isset($this['id']) ? json_decode($this['id'], true) : null;
if ($pk && $this->parent instanceof AbstractBackend) {
return \PropelQuery::from($this->parent->getModelClass())->findPk($pk);
}
return null;
}
示例6: testQuery
public function testQuery()
{
BookstoreDataPopulator::depopulate();
BookstoreDataPopulator::populate();
$book = PropelQuery::from('Book b')->where('b.Title like ?', 'Don%')->orderBy('b.ISBN', 'desc')->findOne();
$this->assertTrue($book instanceof Book);
$this->assertEquals('Don Juan', $book->getTitle());
}
示例7: createIndex
public static function createIndex($class)
{
$index = self::getLuceneIndex($class);
$objects = PropelQuery::from($class)->find();
foreach ($objects as $object)
{
$object->updateLuceneIndex($index);
}
}
示例8: __construct
public function __construct($className, array $options = array(), \ModelCriteria $query = null)
{
$this->className = $className;
$this->options = array_replace_recursive($this->options, $options);
if (!$query) {
$query = \PropelQuery::from($this->className);
}
$this->query = $query;
}
示例9: doClean
/**
* @see sfValidatorBase
*/
protected function doClean($values)
{
if (!is_array($values)) {
throw new InvalidArgumentException('You must pass an array parameter to the clean() method (this validator can only be used as a post validator).');
}
if (!is_array($this->getOption('column'))) {
$this->setOption('column', array($this->getOption('column')));
}
if (!is_array($field = $this->getOption('field'))) {
$this->setOption('field', $field ? array($field) : array());
}
$fields = $this->getOption('field');
$criteria = PropelQuery::from($this->getOption('model'));
foreach ((array) $this->getOption('query_methods') as $methodName => $methodParams) {
if (is_array($methodParams)) {
$criteria = call_user_func_array(array($criteria, $methodName), $methodParams);
} else {
$criteria = $criteria->{$methodParams}();
}
}
if ($this->getOption('allow_null_uniques')) {
$tableMap = call_user_func(array($this->getOption('model') . 'Peer', 'getTableMap'));
$nullColsCount = 0;
}
foreach ($this->getOption('column') as $i => $column) {
$name = isset($fields[$i]) ? $fields[$i] : $column;
if (!array_key_exists($name, $values)) {
// one of the column has been removed from the form
return $values;
}
$colName = call_user_func(array(constant($this->getOption('model') . '::PEER'), 'translateFieldName'), $column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
// handle null unique indexes
if ($this->getOption('allow_null_uniques') && null === $values[$name] && !$tableMap->getColumn($colName)->isNotNull()) {
$nullColsCount++;
continue;
}
$criteria->add($colName, $values[$name]);
}
if ($this->getOption('allow_null_uniques') && $nullColsCount == count($this->getOption('column'))) {
// all columns for checking were both empty and null unique
$object = null;
} else {
// perform query for normal unique check
$object = $criteria->findOne($this->getOption('connection'));
}
// if no object or if we're updating the object, it's ok
if (null === $object || $this->isUpdate($object, $values)) {
return $values;
}
$error = new sfValidatorError($this, 'invalid', array('column' => implode(', ', $this->getOption('column'))));
if ($this->getOption('throw_global_error')) {
throw $error;
}
$columns = $this->getOption('column');
throw new sfValidatorErrorSchema($this, array($columns[0] => $error));
}
示例10: render
public function render(Curry_Backend $backend, array $params)
{
$modelClass = $this->modelForm->getModelClass();
$item = $this->getSelection($params);
if (!isset($item)) {
$item = new $modelClass();
$relatedItem = $this->getParentSelection($params);
if ($relatedItem) {
$relations = PropelQuery::from($modelClass)->getTableMap()->getRelations();
foreach ($relations as $relation) {
if ($relation->getRightTable()->getPhpName() == get_class($relatedItem) && in_array($relation->getType(), array(RelationMap::MANY_TO_ONE))) {
$item->{'set' . $relation->getName()}($relatedItem);
}
}
}
}
$form = clone $this->modelForm;
$form->setOptions(array('method' => 'post', 'action' => (string) url('', $params)));
$buttons = array('save');
$form->addElement('submit', 'save', array('label' => 'Save'));
if (!$item->isNew() && $this->parentView instanceof Curry_ModelView_List && $this->parentView->hasAction('delete')) {
$form->addElement('submit', 'delete', array('label' => 'Delete', 'class' => 'btn btn-danger', 'onclick' => "return confirm('Do you really want to delete this item? This cannot be undone.');"));
$buttons[] = 'delete';
}
$form->addDisplayGroup($buttons, 'save_group', array('class' => 'horizontal-group'));
$form->fillForm($item);
if (isPost() && $form->isValid($_POST)) {
if ($form->delete && $form->delete->isChecked()) {
$backend->createModelUpdateEvent($modelClass, $item->getPrimaryKey(), 'delete');
$item->delete();
if ($item instanceof Curry_ISearchable) {
Curry_Backend_Indexer::removeItem($item);
}
$backend->addMainContent('<p>The item has been deleted.</p>');
return;
}
$form->fillModel($item);
$this->triggerCallback($this->preSave, $item, $form);
$item->save();
$this->triggerCallback($this->postSave, $item, $form);
$form->fillForm($item);
$backend->createModelUpdateEvent($modelClass, $item->getPrimaryKey(), 'update');
if ($item instanceof Curry_ISearchable) {
Curry_Backend_Indexer::updateItem($item);
}
if (isAjax()) {
return '';
}
}
$this->triggerCallback($this->preRender, $item, $form);
$backend->addMainContent($form);
}
示例11: show
public function show(Request $request)
{
$modelClass = $this->modelForm->getModelClass();
$item = $this->getSelection();
if (!isset($item) || !$item instanceof $modelClass) {
$item = new $modelClass();
$relatedItem = $this->parent instanceof AbstractBackend ? $this->parent->getSelection() : null;
if ($relatedItem) {
$relations = \PropelQuery::from($modelClass)->getTableMap()->getRelations();
foreach ($relations as $relation) {
if ($relation->getRightTable()->getPhpName() == get_class($relatedItem) && in_array($relation->getType(), array(\RelationMap::MANY_TO_ONE))) {
$item->{'set' . $relation->getName()}($relatedItem);
}
}
}
}
$form = clone $this->modelForm;
$buttons = array('save');
$form->addField('save', array('type' => 'submit', 'label' => 'Save', 'class' => 'btn btn-primary'));
if (!$item->isNew() && $this->parent instanceof ListView && $this->parent->hasAction('delete')) {
$form->addField('delete', array('type' => 'submit', 'label' => 'Delete', 'class' => 'btn btn-danger', 'onclick' => "return confirm('Do you really want to delete this item? This cannot be undone.');"));
$buttons[] = 'delete';
}
//$form->addDisplayGroup($buttons, 'save_group', array('class' => 'horizontal-group'));
$form->fillForm($item);
if ($request->isMethod('POST') && $form->isValid($request->request->all())) {
if ($form->delete && $form->delete->isChecked()) {
//$this->createModelUpdateEvent($modelClass, $item->getPrimaryKey(), 'delete');
$item->delete();
if ($item instanceof \Curry_ISearchable) {
\Curry_Backend_Indexer::removeItem($item);
}
$this->addMainContent('<p>The item has been deleted.</p>');
return parent::render();
}
$form->fillModel($item);
$this->triggerCallback($this->preSave, $item, $form);
$item->save();
$this->triggerCallback($this->postSave, $item, $form);
$form->fillForm($item);
//$this->createModelUpdateEvent($modelClass, $item->getPrimaryKey(), 'update');
if ($item instanceof \Curry_ISearchable) {
\Curry_Backend_Indexer::updateItem($item);
}
if ($request->isXmlHttpRequest()) {
return \Symfony\Component\HttpFoundation\Response::create('');
}
}
$this->triggerCallback($this->preRender, $item, $form);
$this->addMainContent($form);
return parent::render();
}
示例12: testInstancePoolingReenabled
public function testInstancePoolingReenabled()
{
Propel::enableInstancePooling();
$books = PropelQuery::from('Book')->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)->find($this->con);
foreach ($books as $book) {
}
$this->assertTrue(Propel::isInstancePoolingEnabled());
Propel::disableInstancePooling();
$books = PropelQuery::from('Book')->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)->find($this->con);
foreach ($books as $book) {
}
$this->assertFalse(Propel::isInstancePoolingEnabled());
Propel::enableInstancePooling();
}
示例13: setUpBeforeClass
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
self::$fixturesDirectory = realpath(dirname(dirname(__DIR__)) . '/fixtures');
\Curry_Core::init(array('curry' => array('name' => 'Curry Unit Tests', 'adminEmail' => 'info@currycms.com', 'autoBackup' => false, 'projectPath' => self::$fixturesDirectory, 'migrationVersion' => \Curry_Core::MIGRATION_VERSION, 'template' => array('root' => self::$fixturesDirectory . '/templates', 'options' => array('cache' => false)), 'propel' => array('conf' => self::$fixturesDirectory . '/propel/build/conf/curry-conf.php', 'projectClassPath' => self::$fixturesDirectory . '/propel/build/classes'))));
// Empty database
$con = \Propel::getConnection();
$con->beginTransaction();
try {
foreach (\Curry_Propel::getModels(false) as $model) {
\PropelQuery::from($model)->deleteAll();
}
$con->commit();
} catch (Exception $e) {
$con->rollBack();
throw $e;
}
$setup = new \Curry_Backend_Setup();
$setup->saveConfiguration(array('template' => 'empty', 'admin' => array('username' => 'admin', 'password' => 'admin'), 'user' => array('username' => 'user', 'password' => 'user')));
}
示例14: testToArray
public function testToArray()
{
$books = PropelQuery::from('Book')->setFormatter(ModelCriteria::FORMAT_ARRAY)->find();
$booksArray = $books->toArray();
$this->assertEquals(4, count($booksArray));
$bookObjects = PropelQuery::from('Book')->find();
foreach ($booksArray as $key => $book) {
$this->assertEquals($bookObjects[$key]->toArray(), $book);
}
$booksArray = $books->toArray();
$keys = array(0, 1, 2, 3);
$this->assertEquals($keys, array_keys($booksArray));
$booksArray = $books->toArray(null, true);
$keys = array('Book_0', 'Book_1', 'Book_2', 'Book_3');
$this->assertEquals($keys, array_keys($booksArray));
$booksArray = $books->toArray('Title');
$keys = array('Harry Potter and the Order of the Phoenix', 'Quicksilver', 'Don Juan', 'The Tin Drum');
$this->assertEquals($keys, array_keys($booksArray));
$booksArray = $books->toArray('Title', true);
$keys = array('Book_Harry Potter and the Order of the Phoenix', 'Book_Quicksilver', 'Book_Don Juan', 'Book_The Tin Drum');
$this->assertEquals($keys, array_keys($booksArray));
}
示例15: show
public function show(Request $request)
{
$modelClass = $this->modelClass;
$items = array();
if ($this['id'] === ':id' && $request->query->has('item')) {
$pks = array_map(function ($i) {
return json_decode($i, true);
}, $request->query->get('item', array()));
if ($pks && $this->parent instanceof AbstractBackend) {
$items = \PropelQuery::from($this->parent->getModelClass())->findPks($pks)->getArrayCopy();
}
} else {
$items = array($this->getSelection());
}
$items = array_filter($items, function ($item) use($modelClass) {
return $item instanceof $modelClass;
});
if (!count($items)) {
throw new \Exception('No item to delete');
}
$names = array_map(function ($item) {
return method_exists($item, '__toString') ? '`' . htmlspecialchars((string) $item) . '`' : 'this item';
}, $items);
if ($request->isMethod('POST') && $request->request->get('do_delete')) {
foreach ($items as $i => $item) {
$pk = $item->getPrimaryKey();
$item->delete();
// Trigger update event
//$this->createModelUpdateEvent($this->modelClass, $pk, 'delete');
if ($item instanceof \Curry_ISearchable) {
\Curry_Backend_Indexer::removeItem($item);
}
}
$this->addMainContent('<p>' . $names[$i] . ' has been deleted.</p>');
} else {
$this->addMainContent('<form method="post">' . '<input type="hidden" name="do_delete" value="1" />' . '<p>Do you really want to delete ' . join(', ', $names) . '?</p>' . '<button type="submit" class="btn btn-danger">Delete</button>' . '</form>');
}
return parent::render();
}