Zend framework fetchall function - php

I've got below function in my Organisation model.
public function getOrganisations($where=null,$order='name ASC',$offset=null,$limit=null){
$Result = $this->fetchAll($where,$order,$limit,$offset);
if (!$Result){
return array();
}
return $Result->toArray();
}
but How can I include my organisation_types model so I can left join to organisation_type_id in organisation table?

This is the core of the argument in favor of using the data mapper pattern.
With the structure you seem to be using in your example you'll have huge trouble trying pass the organisation_types object into your Organisation model. You can however do a join in your query to join to the organisation_types table, but joining on the object is not likely to be reasonable.
to join on the organisation_types table:
//assuming this is a DbTable model that extends Zend_Db_Table_Abstract
function getOrganisations($where=null,$order='name ASC',$offset=null,$limit=null){
$select = $this->select()->setIntegrityCheck(FALSE);//This locks the table to allow joins
$select->joinLeft('organisation_types', 'organisation_types.id = organisation.organisation_type_id');//This will join the tables with all feilds, use the join type you like.
if (!is_null($where) {
$select->where($where);
}
if (!is_null($order) {
$select->order($order);
}
if (!is_null($offset) {
$select->limit(null,$offset);//offset is second arg in limit() in select()
}
if (!is_null($limit) {
$select->limit($limit);
}
$Result = $this->fetchAll($select);
if (!$Result){
return array();
}
return $Result->toArray();
}
This should give you an idea of how a table join would work. If you want to use the objects you'll need to begin again with a different structure.
I found a couple of good tutorials on PHPMaster that helped me get my head around the data mappers and domain models.
Building A Domain Model, Introduction
Integrating data mappers
Also The online book Survive The Deepend has a good example of the data mapper pattern and how to test it.
Good Luck...

Maybe using Zend_Db_Select with joinLeft() would be more appropriate:
http://framework.zend.com/manual/en/zend.db.select.html#zend.db.select.building.join

Related

Fetch data from many to many relationship with eloquent

I have 2 models, store and dvd with many to many relationship
dvd model:
public function stores() {
return $this->belongsToMany('App\store');
}
store model:
public function dvds ( ) {
return $this->belongsToMany('App\dvd');
}
Then, in controller, when I need fetch data from both models, I use code like this:
$res_store = store::orderBy("id","desc")->get();
foreach( $res_store as $row ) {
$dvds = $row->dvds()->get();
foreach ($dvds as $dvd) {
// echo columns here
}
}
This works, but my question is, I'm doing this in correct way? I'm asking because it seems 2 loops for this simple relation is somehow inefficient.
No, this is not the right way.
When looping over a $store, you can access the associated $dvd records via $store->dvds; there is no need to call $row->dvds()->get(), as that is executing a new query with the same result of $store->dvds. Full code should simply be:
$stores = Store::with("dvds")->orderBy("id", "DESC")->get();
foreach($stores AS $store){
foreach($store->dvds AS $dvd){
... // Do something with `$dvd`
}
}
The ::with("dvds") clause is known as "Eager loading", and prevents $store->dvds from needing execute another query behind the scenes.
Also, please name your models correctly. Classes in PHP are StudlyCase, so Store and DVD, not store and dvd.

I want to retrieve the instance of a course module

tl;dr; trying to get a course module instance via a course module through mysql or the official moodle api. My solution in the end seems rather scrappy though, so i would like some advice.
Hi Moodle people
I am trying to retrieve the corresponding course module instance for each row in mdl_course_modules, a instance in this context would be a single row from mdl_quiz, mdl_wiki or some other course_modules instance.
I have tried a few things, the one i expected to work was:
function get_all_course_modules($course_id) {
global $DB;
$course = $DB->get_record('course', array('id' => $course_id));
$mod_info = get_fast_modinfo($course);
}
I wasn't able to get the course modules instance from $mod_info .
So i tried to do some custom mysql instead,
SELECT course.id, course.fullname, cm.course, cm.section, m.name
FROM mdl_course AS course
JOIN mdl_course_modules AS cm ON cm.course = course.id
JOIN mdl_modules AS m ON m.id = cm.module
JOIN CONCAT('mdl_', m.name) AS module_type ON ...somethingsomething
WHERE course.id = 2;
The sql isn't quite finished, i stumbled on the fact that i dont know all of the different course modules beforehand, that means that i have to use the name of the course_module dynamically to select which instance table i want to join on, or just LEFT JOIN on all of the possible course modules instance tables, but that would take forever and would stop working if someone put in a new course module instance.
Right now im doing the following:
$result = array();
$course_mods = get_course_mods($course_id);
if($course_mods) {
foreach($course_mods as $course_mod) {
$DB->get_records_sql('some sql that selects the instance');
}
}
return $result;
This last piece of code would eventually work, i would be able to able to dynamically create the query from the $course_mod variables such as $course_mod->instance, $course_mod->mod_name etc.
But i think this seems tough. Have any of you guys suceeded in getting the course_module instance in a easier way??
Thanks!!
This is the function i ended up using, to retrieve course module instances, its not so pretty, but whatever.
function get_all_course_modules($course_id) {
global $DB;
$course_mods = get_course_mods($course_id);
$result = array();
if($course_mods) {
foreach($course_mods as $course_mod) {
$course_mod->course_module_instance = $DB->get_record($course_mod->modname, array('id' =>$course_mod->instance ));
$result[$course_mod->id] = $course_mod;
}
}
return $result;
}

Zend framework current method to get single row

i'm new to zend framework, in this simple function i want to get a single 'post' and then i want to find all the comments in the related table
public function getPost($idPost)
{
$db= Zend_Registry::get('db');
$select=$db->select()
->from($this->_name, '*')
->where("idPost= ".$db->quote($idPost, 'INTEGER'));
$stmt=$select->query();
$rowset=$stmt->fetchAll();
$post=$rowset->current();
//ora devo aggiungerci i commenti che questo post ha ricevuto
$comm=$post->findDependentRowset('commenti');
$ris=array($post, $comm);
return $ris;
}
in my index controller i i simply call this function, but i get this error:
Call to a member function current() on a non-object in C:\xampp\htdocs\...
where's the mistake?
I think you have a few misconceptions about how you're using Zend_Db.
1. You're not using the ORM, just the PDO wrapper
Which means, your queries won't return Zend rowsets and rows and therefore you can't use the methods of you can use on those.
2. The default fetch mode
The default fetch mode of the Zend_Db_Statement fetchAll() method is array, if you want it to return an object (stdClass), change the fetch mode before fetching the data:
$stmt->setFetchMode(Zend_Db::FETCH_OBJ);
3. Using fetchAll() when you actually want one row
If you just want one row, then don't fetch a whole table! With Zend_Db_Statement, use for example:
$row = $stmt->fetch();
or
$rowObj = $stmt->fetchObject();
... again, that's not a zend row object, just a stdClass instance, but you can do:
$rowObj->some_field;
on it.
On the other hand, if this is a method in your Post model, it should look something like:
public function getPost($idPost)
{
return $this->getRow($idPost);
}
This will return the post, then, if you've setup the table relationships correctly, you can also query for the dependent data or just get all comments with that id separately.
The problem is that unless you define a table class as was previously mentioned you can't uuse the dependent or parent rowsets.
To make your current function work would be best done with two functions, and keep it simple:
public function getPost($idPost)
{
$db= new Zend_Db_Table($this->_name);
$select=$db->select()
->where("idPost= ?", $idPost);
/*Fetch just the row you want, or use fetchAll() if you need to match return types*/
$row = $db->fetchRow($select);
return $row;
}
public function getComments($table='comments', $id) {
$db = new Zend_Db_table($table);
$select = $db->select()->where('post_id = ?', $id)->order('date ASC');
$rowset = $db->fetchAll($select);
return $rowset/* or you could return an array ->$rowset->toArray() */
}
Zend_Db_Table is going to attempt to use the current database adapter, so all you need to do is pass in the tablename.
One more note: you don't need to use any of the quote() function when using select() it's taken care of.
But it is really important, that if you are going to use Zend_Db, you need to learn about "Defining table classes". At least enough to use them in your own classes.
I hope this helps!
To get a rowset and dependent rowset you have to use Zend_Db_Table.
You only use the Zend_Db_Adapter with Zend_Db_Select.
Read from here.
So you have to define a class which extends from Zend_Db_Table_Abstract.
Example:
class Bugs extends Zend_Db_Table_Abstract
{
protected $_name = 'bugs';
protected $_primary = 'bug_id';
}
To get the Zend_Db_Table_Rowset object use:
$bugs = new Bugs();
$rowset = $bugs->fetchAll("bug_status = 'NEW'");
To find dependent rowsets you have to define the relation in your table class. Look here how to define relationships.

Doctrine/Symfony 2: Entities with too many relationships

I'm working with Entity objects from Doctrine queries and i end up with a very big array, with all information from all entities related. This ends up being a huge data tree... how can i limit this? Avoid listing all data from all relationships?
You can always remove not needed associations (this is a best practice for speeding up Doctrine). Or you can select only fields that you need in your presentation layer (as read-only data):
public function getAll()
{
$qb = $this->createQueryBuilder('u'); // Where are in User custom repository
return $qb
->select(array('u.id', 'u.first', 'u.last'))
->getQuery()
->getResult();
}
If you still need to work with objects (or for complex queries that needs plain SQL) a possibility is filling only needed properties (and eventually, associations/nested collections) of your domain object.
An example, more on native SQL:
public function getAll()
{
$mapping = new \Doctrine\ORM\Query\ResultSetMapping();
$mapping->addEntityResult('Acme\HelloBundle\User', 'e');
$mapping->addFieldResult('e', 'id', 'id');
$mapping->addFieldResult('e', 'first', 'first');
$mapping->addFieldResult('e', 'last', 'last');
$sql = "SELECT id, first, last FROM user ";
$result = $this->_em->createNativeQuery($sql, $mapping)->getResult();
// Or hust return $result itself (array)
return new \Doctrine\Common\Collections\ArrayCollection($result);
}
Of course the disadvance (?) is use of native SQL. I don't believe that ResultSetMapping can be used with DQL.
EDIT: take a look at http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/best-practices.html

Zend_Db_Select order by random, compatible in mssql / mysql

Alright here's the situation, I have an application written in the Zend_Framework, that is compatible with both MySQL and MSSQL as the backend. Now, ZF is pretty good at solving a lot of the SQL discrepancies/differences between the two languages, but I still have yet to figure this one out.
The objective is to select 1 random record from the table, which is an extremely simple statement.
Here's a select statement for example:
$sql = $db->select()
->from("table")
->order("rand()")
->limit(1);
This works perfectly for the MySQL database tables, because the sql for MySQL is as follows:
SELECT `table`.* FROM `table` ORDER BY rand() ASC
Now MSSQL on the other hand, uses the newid() function to do randomizing.
Is there some sort of helper I can pass into the order() function in order to make it realize that it has to use the proper ordering? I searched the documentation and on the zfforums, found a few tips, but nothing solid.
One of the things I did find was:
ORDER BY RANDOM() not working - ZFForums.com
They are using the following:
$res = $db->fetchAll(
'SELECT * FROM table ORDER BY :random',
array('random' => new Zend_Db_Expr('RANDOM()')
);
It works... but I am not looking to build my select statement by typing it out and doing a replace on the string, I am trying to keep it in the same Zend_Db_Select object. I also have tried passing in the Zend_Db_Expr('RANDOM()') into the ->order() on the statement, and it fails. He also posts a theoretical solution to finding the answer, but I am not looking to rewrite the function this is within, modifying the $db->fetch() call.
Any ideas?
You could quickly abstract the function to a table - who knows which adapter it is using:
class MyTable extends Zend_Db_Table_Abstract {
public function randomSelect($select=null) {
if ($select === null) $select = $this->select();
if (!$select instanceOf Zend_Db_Select) $select = $this->select($select);
$adapter = $this->getAdapter();
if ($adapter instanceOf Zend_Db_Adapter_Mysqli) {
$select->order(new Zend_Db_Expr('RAND()'));
} else if ($adapter instanceOf Zend_Db_Adapter_Dblib) {
$select->order(new Zend_Db_Expr('NEWID()'));
} else {
throw new Exception('Unknown adapter in MyTable');
}
return $select;
}
}
$someSelect = $table->select();
// add it to an existing select
$table->randomSelect($someSelect);
// or create one from scratch
$select = $table->randomSelect();
Also, I found an article somewhere which I lost that recommended trying something like:
$select->order(new Zend_Db_Expr('0*`id`+RAND()));
to subvert MSSQL's query optimizer and trick it into calculating a new value for each row.
I would create class My_Db_Expr_Rand extends Zend_Db_Expr. Bassed on the adapter I would return either one or the other.

Categories