I have used the following query in Silverstripe 2.x projects:
$obj = DataObject::get_one('Post', "\"URLSegment\" = '$segment'")
Does anyone know how I would replicate this query in the new ORM?
I have tried :
$obj = Post::get()->filter("\"URLSegment\" = '$segment'")
$obj = Post::get()->where("\"URLSegment\" = '$segment'")
And neither appear to work.
Thanks!
The issue you're having is that DataObject::get() will return a collection of objects, whereas DataObject::get_one() will return a single object of the specified class. The Silverstripe 3 equivalent is:
$obj = Post::get()->where("\"URLSegment\" = '$segment'")->First();
With this scenario (as with SS2) you would need to make sure you're explicitly taking care escaping to avoid injection vulnerabilities. However the filter function will now take care of that for you. So what you really want now is:
$obj = Post::get()->filter('URLSegment', $segment)->First();
The related documentation is at: http://doc.silverstripe.org/framework/en/topics/datamodel
DataList::create('Post')->filter(array('URLSegment' => $segment))->First();
will also work.
Related
I'm trying to build something based on options, and I use a QueryBuilder to make a request with JOIN and ORDER
Here's a simple example of code I could produce :
$query = $this->createQueryBuilder('s')
->leftJoin('s.ville', 'v')
->andWhere('s.name = :name')->setParameter('name', 'test')
->orderBy('s.id');
This lead to an exception... "Expected Literal, got 'ORDER'" 'cause in the final request, the WHERE clause is empty...
Any idea ?
Just do a where ? andWhere mean you have many where in your query :).
(Take care I think you want the name of the 'ville', because of your leftJoin).
$query = $this->createQueryBuilder('s')
->leftJoin('s.ville', 'v')
//v.name ?
->where('s.name = :name')->setParameter('name', 'test')
->orderBy('s.id');
I have the same problem using doctrine/orm < 2.4. In previous versions you had to use where() before an andWhere(). In Doctrine docs from version 2.5 they say:
// NOTE: ->andWhere() can be used directly, without any ->where() before
However, It's fixed in version 2.4.8 too, so you could use that version too.
I feel like this shouldn't be to hard, but try as I might, I keep getting errors.
What I'm wanting to do, is to have a single "add" function that will handle the basic functionality of adding records to any / all tables. Basically, the post data will contain the table name, as well as the fields / values to be inserted. The controller itself, confirms the user has access to do these things, and then verifies the fields are valid, before creating a new instance of the entity, that's where things go haywire:
$entityName = 'Products';
$row = new $entityName(); //doesn't work
$row new Products(); //works
I haven't found a way or any examples of creating a new entity using the Entity Manager, or else that might work, because i've created functions using EM to do queries, updates, and deletes, but I just can't get this to work with the add functions.
1. Your problem is almost certainly namespacing (see below). Try this instead:
$entityName = 'My\Bundle\Entity\Products';
$row = new $entityName();
Should work. ;)
2. If you want to create new instances using an EntityManager, try this:
$entityName = 'My\Bundle\Entity\Products';
$row = $em->getClassMetadata($entityName)->newInstance();
...assuming $em is your EntityManager. :-)
3. To "prove" that your problem is namespacing, try this in a plain .php file that you run from the command line:
namespace Foo;
class Test {}
$class1 = 'Foo\Test';
$class2 = 'Test';
$x = new Test(); // Will work
$y = new $class1(); // Will work
$z = new $class2(); // Will trigger "Class 'Test' not found" fatal error
Two things:
Try with "Products" instead of 'Products'
I suppose that your entity Products has a namespace, it is required (even if you declared an use statement). So try with "My\Bundle\Entity\Products".
I haven't found any documentation for setting field attributes to for Zend Framework 2 form collection. I can set the value of a single input field like this:
$form->get('title')->setAttribute('value', $value);
What I can't figure out is how to set the values for a collection.
$form->get('sample_collection') returns a Zend\Form\Element\Collection Object
It seems like I need to go one layer deeper and select the specific field so that I can use the ->setAttribute on it.
Thank you in advance for your help in solving this.
I had some real trouble with this, the only way I could actually get access to a fieldset inside a collection was with the following. (If the collection has more then one fieldset you would have to add an if statement inside the foreach loop to get the fieldset you want.)
$array = array('keys'=>'values');
$collection = $form->get('name_of_collection');
foreach ($collection as $coll)
{
$fieldset = $coll;
}
$element = $fieldset->get('name_of_element');
$element->setValueOptions($array);
I expected the following to work, which does not. I am not sure if this is a bug in the Zend framework or if I am doing something wrong.
$collection = $form->get('name_of_collection');
$fieldset = $collection->get('name_of_fieldset');
$element = $fieldset->get('name_of_element');
If you just want to access a single element inside a fieldset NOT inside a collection. The following has worked fine for me.
$fieldset = $form->get('name_of_fieldset');
$element = $fieldset->get('name_of_element');
$element->setAttribute('id', 'name_of_element');
I hope this can be of help to someone.
Use form collection as an array:
$elements = $form->get('sample_collection');
foreach($elements as $element){
$element->setAttribute('value', $value);
}
Using doctrine for the first time in a project, and I'm having a few issues with the query builder.
First up, in a controller I used the following:
$conn = $this->get('database_connection');
$users = $conn->fetchAll('SELECT * FROM Users');
This worked fine and returns an array of users from my DB.
I then tried to use the query builder to fetch the forenames of all users. By looking at examples i found the following:
$conn = $this->get('database_connection');
$qb = $conn->createQueryBuilder();
$qb->select("forename")
->from("Users", "u")
->where("u.id = :user_id")
->setParameter('user_id', 1);
$query = $qb->getQuery();
$results = $query->getResults();
I get told that the gDoctrine\DBAL\Query\QueryBuilder::getQuery() method is not defined, which I found odd as almost all the examples I found use it.
I did a search and found Doctrine documentation but I'm now confused how to use it at all.
Would someone be kind enough to give me an example of how to use the above to retrieve the forename for the User with id 1. I'm sure that once I have a simple example that work I will be fine from there.
Thanks!
Now Resolved:
After looking at the Documentation (and with help from others), I found the general layout for the queryBuilder is as follows:
$conn = $this->get('database_connection');
$qb = $conn->createQueryBuilder();
$stmt = $qb->select("forename")
->from("Users", "u")
->where("u.id = :user_id")
->setParameter('user_id', 1)
->execute();
$userNames = $stmt->fetchAll();
The general idea being that the execute method returns a Doctrine\DBAL\Driver\Statement, with the parameters set as specified. From this statement you can call one of the various method stated here to get the results from the DB.
Hope this helps someone else who is having problems!
I think you might be confused slightly by the documents on doctrine and using it with symfony. From the error message you are getting a DBAL\Query\QueryBuilder. There is not a method getQuery for that object. You should be able to use the execute method to get the results you want.
Now with that being said the way I would do it is through either a repository class for the Users entity, or I would do
$em = $this->get('doctrine.orm.entity_manager');
$qb = $em->createQueryBuilder();
then do what you were doing before...
I would like to build a custom search module in Drupal 6 for searching through CCK. I need the user to search between his nodes (node.uid=x) and of a certain type (type='xyz'). I think I have to implement hook_search but I don't know where to put my filters. Can anyone help me?
You already accepted an answer (which is probably the best option for you), but there are a few other ways to accomplish this.
IIRC, the Custom Search module will work for what you want.
You can copy the stock hook_search function to a custom module and modify the query. You can do something like this:
// ...
case 'search':
// Build matching conditions
list($join1, $where1) = _db_rewrite_sql();
$arguments1 = array();
$conditions1 = 'n.status = 1';
// NEW BIT START
$allowed = array(
'content_type_1',
'content_type_2',
'content_type_3',
);
$types = array();
foreach ($allowed as $t) {
$types[] = "n.type = '%s'";
$arguments1[] = $t;
}
$conditions1 .= ' AND ('. implode(' OR ', $types) .')';
$keys = search_query_insert($keys, 'type');
// NEW BIT END
This replaces the bit that extracts the type from the actual query string.
You would have to add in the bit to restruct to a particular n.uid. I have been using this method lately, rather that Custom Search, because it simpler from the user's perspective.
HTH
You might try creating a Views with an exposed filter, it's the absolute easiest way to implementing your idea.
Also you can try use CCK Facets. But Views - of course simple.