How to select Eloquent model without arguments - eloquent

I don't know if 'arguments' is the right term. I want to get a model without everything and add the selects and wheres later.
$query = Pic::;
if($cat) $query->where('cat',$cat);
if($year) $query->where('jahrprod',$year);
$query->get();
Pic:: or Pic don't work. Only DB::table('pics') does, but does not seem to return a collection.

Use Model::query();
$query = Pic::query();
if($cat) $query->where('cat',$cat);
if($year) $query->where('jahrprod',$year);
$query->get();

Related

How to do a nested WHERE in Zend Framework 1 using Zend_Db_Select with AND and OR operators?

So... I've inherited this rather large project based on Zend Framework 1.12 and a feature that I'm trying to add involves a more complex database operation than what I'm used to doing with the project. I'm extremely new to Zend Framework, or MVC for that matter. I found an answer for Zend Framework 3 which would have been perfect but I have to make do with this version.
The function basically builds a Zend_Db_Select based on various parameters and the feature I'm trying to add will involve joining two different tables and checking if a specific combination exists in one or the other.
Here's what I have so far:
//SQL that I'm trying to do. Assume table1 and table2 are already joined.
//Ignore the imperfect syntax. I'm trying to get the concept across.
//SELECT * FROM (table1 joined to table2 by a common key)
//WHERE ( (table1.column1 = myParam1) AND (table1.column2 = myParam2) )
//OR WHERE ( (table2.column1 = myParam1) AND (table2.column2 = myParam2) )
public function buildSelect($params){
//Zend code starts here
//This one starts the Zend_Db_Select
$select = $this->select();
$table1Name = get_table_name_from_object($table1);
//lots of preexisting code here
//my code starts here.
$table2Name = get_table_name_from_object($table2);
$select->join($table2Name, "$table1Name.key = $table2Name.key", array('column1', 'column2', 'key');
//After I wrote this, I instantly realized why it won't work the way I intended it but putting it here to show what I tried at which point I got stuck.
$select->where("($table1Name.column1 = ?) OR ($table2Name.column1 = ?)",$params[1]);
$select->where( "($table1Name.column2 = ?) OR ($table2Name.column2 = ?)", $params[2]);
//more preexisting code below.
return $select
}
Obviously, if I tried this as is, the program will happily return results that include a combination of, say, an entry where param1 is in table1.column1 and param2 is in table2.column2.
I received some feedback from a friend and posting here for posterity.
They noticed my code already contains parentheses and recommended that I simply take advantage of orWhere() then write it like this:
$select->where("($tableName1.column1 = ?", $params[param1])
->where("$tableName1.column2 = ?)", $params[param2]);
$select->orWhere("($tableName1.column1 = ?",$params[param1])
->where("$tableName2.column2 = ?)",$params[param2]);

Query Builder not working if expression was separated into a few expressions

I'm woking with the last version of the Doctrine ODM (Mongodb).
This works:
$items = $om->createQueryBuilder($itemClass)
->field('active')->equals(true)
->getQuery()->execute();
This doesn't work:
$items = $om->createQueryBuilder($itemClass)
->field('active')->equals(true);
$items->getQuery()->execute();
I need it to be working If I want to add dynamic parameters. Both query builders executes exactly the same query (shown in the profiler).
Am I doing something wrong or thi is a doctrine bug?
It looks like you simply forgot to assign the result of execute() back to $items:
$items = $om->createQueryBuilder($itemClass)
->field('active')->equals(true);
$items = $items->getQuery()->execute();

Easy way to get "params" from Joomla menu table

I don't know too much about Joomla, but I'm trying to work with a Menu on a Joomla site. In the Database I can see a column called params in the menu table, and it has some data I need. The params column has this data:
categories=446
feedLink=1
fusion_item_subtext=
fusion_columns=1
fusion_customimage=
splitmenu_item_subtext=
page_title=
show_page_title=1
pageclass_sfx=
menu_image=-1
secure=0
I know I can do a mysql query, get that column and parse the value using string manipulation/regex, but that doesn't sound like the right way.
I have seen some code in Joomla that looks like:
$cid = $params->get('secure');
Does Joomla have a special way to query and return objects so that these params are accessible with this type of syntax?
Right way is to use JMenu::getParams method
$app =& JFactory::getApplication();
$menu =& $app->getMenu();
$params = $menu->getParams($menuItemId);
$params->get('paramName');
Yes, Joomla does have special way of getting the parameters in an easily accessible object based on JObject.
you can get the entire site menu with this
$menu = JFactory::getApplication()->getMenu();
$item = $menu->getActive(); // will get active menu item. can use getItem() instead to get specific item
$item->get('parmName');
This is not exact code, more like pseudocode. This will get you on the right track...
Helpfull Stuff:
Joomla Framework API
JMenu Documentation
first you get a JApplication instance like this
$app = & JFactory::getApplication();
or for joomla 1.5 use:
global $mainframe //to get JApplication object
get JMenu instance like this:
$menu = $app->getMenu();
you can get active menu params or any other menu params like this
$active = $menu->getActive(); //get active menu
$menuInstance = $menu->getActive($Itemid); // to get Itemid use JRequest::getInt('Itemid', 0);
here you have an StdClass object with params field inside, now u use JParameter class like this
$menuParams = new JParameter($menuInstance->params);
here you have it, to get any parameter you want:
$someParam = $menuParams->get('some_param', 'default');

Using data mappers in Zend Framework

For example:
I need to retrieve a set of male User's IDs, First Names, and Last Names, but nothing else.
So I have a function in UserMapper called fetchAllMaleUsers() that returns a set of User entities.
i.e:
public function fetchAllMaleUsers() {
$select = $this->getDbTable()
->select()
->from($this->getDbTable(),
array('ID', 'FirstName', 'LastName'))
->where('Gender = ?', 'M');
$resultSet = $this->getDbTable()->fetchAll($select);
$users = array();
foreach ($resultSet as $row) {
$user = new Application_Model_User();
$user->setId($row->ID)
->setFirstName($row->FirstName)
->setLastName($row->LastName);
$users[] = $user;
}
return $users;
}
Does this function belong in the mapper layer?
Is it ok to only set the Id, Firstname, and LastName of each User entity?
1) Perfectly fine for the mapper/Gateway or however you call it.
2) You can do but i highly disencourage this. Why? Later on in your application you can't tell from where you did get the model. So you'd have to check if a value is set in your model each time you're not sure if it is set or you'd need some autoloading stuff for missing values (which is as worse as missing stuff). Another reason is that you can't reuse the function for other porposes where you might need other properties of the user model. Last reason afaik is, that a model represents the complete entity at any time, not parts of it. And there's no real reason why not to load all fields (beside references to other entities why should be autoloaded anyways).
Along with Fge's reply I believe that $resultSet should already be returning values of type Application_Model_User. If not, you may need to set $_rowClass in Application_Model_DbTable_User.

findManyToManyRowset with Zend_Db_Table_Select

I'm trying to use a select object to filter the results of a many to many rowset. This call works great:
$articles = $this->model->findArticlesViaArticlesUsers();
This however does not:
$articles = new Default_Model_Articles();
$articleSelect = $articles->select();
$articleSelect->where("status = 'published'")
->order("date_published DESC")
->limit(1);
$articles = $this->model->findArticlesViaArticlesUsers($articleSelect);
That throws the following error:
exception 'Zend_Db_Select_Exception'
with message 'You cannot define a
correlation name 'i' more than once'
I can't figure out how to successfully get "articles that have the status of 'published'" using the magic many-to-many relationship (nor findManyToManyRowset). I'm at the end of my rope and thinking of just writing the sql manually. Any ideas?
When defining the select statement, you must use the same object that you call findManyToManyRowset (or whatever magic function you use) on.
Ex:
$articles = new Default_Model_Articles();
$user = $articles->find($userId)->current();
$select = $user->select();
$select->where('status = ?', 'published');
$articles = $user->findArticlesViaArticlesUsers($select);
Notice the select statement and findArticlesViaArticlesUsers are both extending $user. Thats the key.
I think you've misunderstood how the relationships work.
See this manual page - you should call the magic method, findArticlesViaArticlesUsers, on a Row object. In this case, I think you want to find a User, and then call findArticles... on that.