Using fputcsv printing array instead of values when with Doctrine and Zend Framewrok
Hi I'm using Doctrine as my ORM for my Zend App.
I was looking at exporting a query to a csv file however I get the following output
44 Brownie Don't feed after 8pm Array Array
So the pet name and their requirements are printing out (along with their ID for some reason) but the pet's breed and behavior are simply printing as ARRAY.
I realize that this is due to the query as the behavior and breed information are stored within their own array within the result array but how can I get these values to export to the file. I've tried adding where clauses to the query to match the pets breed and behavior to the primary keys of the related table but it did not work so I removed them.
The code for my action is below
开发者_Python百科public function todayscatterybookingstofileAction()
{
$today=date('y-m-d');
$q = Doctrine_Query::create()
->select('p.name,b.breed,h.behaviour,p.special_requirements')
->from('PetManager_Model_Pets p')
->leftJoin('p.PetManager_Model_Breeds b')
->leftJoin('p.PetManager_Model_Behaviour h')
->leftJoin('p.PetManager_Model_Catterypets s')
->leftJoin('s.PetManager_Model_Catterybooking k')
->where('s.bookingID = k.catterybookingID')
->andWhere('p.petID=s.pet')
->andwhere('k.srtDate < ?',$today)
->andWhere('k.edDate >= ?',$today)
->andWhere('k.catteryappointmentstatus=1');
$result = $q->fetchArray();
$description="D:/reports/Occupied Catteries";
$extension=".csv";
$filename=$description.$today.$extension;
$file = fopen($filename,"w");
foreach($result as $line)
{
fputcsv($file,$line);
}
fclose($file);
}
If you are using the Doctrine 1.1 or higher, you may be able to use the HYDRATE_SCALAR option to force a flat array:
$q = Doctrine_Query::create()
->select('p.name,b.breed,h.behaviour,p.special_requirements')
->from('PetManager_Model_Pets p')
->leftJoin('p.PetManager_Model_Breeds b')
->leftJoin('p.PetManager_Model_Behaviour h')
->leftJoin('p.PetManager_Model_Catterypets s')
->leftJoin('s.PetManager_Model_Catterybooking k')
->where('s.bookingID = k.catterybookingID')
->andWhere('p.petID=s.pet')
->andwhere('k.srtDate < ?',$today)
->andWhere('k.edDate >= ?',$today)
->andWhere('k.catteryappointmentstatus=1')->setHydrationMode(Doctrine::HYDRATE_SCALAR);
and that should give you a flat list.
精彩评论