I am using October CMS built on Laravel, and I am having some strange issues I am not sure how to interpret.
Code Sample 1: Works fine (Component EstateList)
public function onRun()
{
$this->listEstates();
}
/**
* Pulls all the estates from the model
*
* #return $estateList
*/
protected function listEstates()
{
$estateList = RealEstate::all();
return $estateList;
}
Code Sample 2: Doesn't work (Component EstateDetails)
public function onRun()
{
$this->show();
}
/**
* Returns the slug and display individual Estate Object
*
* #return $pageDetails
*/
protected function show()
{
$slug = $this->param('slug');
$pageDetails = RealEstate::find($slug);
echo $slug; //returns slug as it should
echo $pageDetails; //empty
if ($pageDetails) {
return $pageDetails;
} else {
return \Response::make('Page not found', 404);
}
}
If I just put the code of show() into the function onRun() it works fine. Why does echo echo $pageDetails return empty on the Code Sample 2? if it is ran in a seperate function show().
Thank you for your help.
Try changing it to RealEstate::where('slug', '=', $slug)->firstOrFail();. The find bit searches the ID table for the column, not the slug.
You are not 'returning' the Response from show()
Try changing $this->show(); to return return $this->show(); in onRun()
Try changing your code to this
public function onRun()
{
return $this->show();
}
/**
* Returns the slug and display individual Estate Object
*
* #return $pageDetails
*/
protected function show()
{
$slug = $this->param('slug');
$pageDetails = RealEstate::where('slug', '=', $slug)->firstOrFail();;
echo $slug; //returns slug as it should
echo $pageDetails; //empty
if ($pageDetails) {
return $pageDetails;
} else {
return \Response::make('Page not found', 404);
}
}
Hopefully This will solve your problem
Related
I'm trying to make reusable datatable instance
My Datatable Class :
class Datatables extends CI_Model {
protected $columnOrder;
protected $columnSearch;
protected $query;
public function __construct($columnOrder,$columnSearch,$query)
{
parent::__construct();
$this->columnOrder = $columnOrder;
$this->columnSearch = $columnSearch;
$this->query = $query;
}
/**
* Generate db query
*
* #return object
*/
private function getDatatablesQuery()
{
$i = 0;
foreach ($this->columnSearch as $item) {
if(#$_POST['search']['value']) {
if($i===0) {
$this->query->group_start();
$this->query->like($item, $_POST['search']['value']);
} else {
$this->query->or_like($item, $_POST['search']['value']);
}
if(count($this->columnSearch) - 1 == $i)
$this->query->group_end();
}
$i++;
}
if(isset($_POST['order'])) {
$this->query->order_by($this->columnOrder[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
} else if(isset($this->order)) {
$order = $this->order;
$$this->query->order_by(key($order), $order[key($order)]);
}
}
/**
* Generate db result
*
* #return integer
*/
public function getDatatables()
{
$this->getDatatablesQuery();
if(#$_POST['length'] != -1) $this->query->limit(#$_POST['length'], #$_POST['start']);
$query = $this->query->get();
return $query->result();
}
/**
* Count filtered rows
*
* #return integer
*/
public function countFiltered()
{
$query = $this->query->get();
return $query->num_rows;
}
/**
* Count all rows
*
* #return integer
*/
public function countAll()
{
return $this->query->count_all_results();
}
}
My FmrTable Class
<?php defined('BASEPATH') OR exit('No direct script access alowed');
require 'application/libraries/Datatables/Datatables.php';
class FmrTable {
protected $select;
protected $columnOrder;
protected $columnSearch;
protected $ci;
public function __construct()
{
$this->select = 'fmrs.id as id,sections.name as section,users.username as user,fmr_no,fmrs.status';
$this->columnOrder = ['id','section','user','fmr_no','status'];
$this->columnSearch = ['section','user','fmr_no','status'];
$this->ci = get_instance();
}
public function get()
{
$query = $this->ci->db
->select($this->select)
->from('fmrs')
->join('sections as sections', 'fmrs.section_id = sections.id', 'LEFT')
->join('users as users', 'fmrs.user_id = users.id', 'LEFT');
$query->where('section_id',$this->ci->session->userdata('section-fmr'));
}
$datatable = new Datatables($this->columnOrder,$this->columnSearch,$query);
return [
'list' => $datatable->getDatatables(),
'countAll' => $datatable->countAll(),
'countFiltered' => $datatable->countFiltered()
];
}
}
This always throw a database error that says Error Number: 1096 No tables used
This came from the countFiltered() method, when i tried to dump the $query without get(), it returned the correct object instance but if i do this then the num_rows property will never available, but when i add the get() method, it will return the 1096 error number
How to solve this ?
A call to ->get() resets the query builder. So when you call ->get() for the second time (in countFiltered) the table name and the remainder of the query have been cleared and that's why you get the error.
Solution is to use query builder caching. This allows you to cache part of the query (between start_cache and stop_cache) and execute it multiple times: https://www.codeigniter.com/userguide3/database/query_builder.html?highlight=start_cache#query-builder-caching
Use flush_cache to clear the cache afterwards, so the cached query part does not interfere with subsequent queries in the same request:
FmrTable
public function get()
{
$this->ci->db->start_cache();
$query = $this->ci->db
->select($this->select)
->from('fmrs')
->join('sections as sections', 'fmrs.section_id = sections.id', 'LEFT')
->join('users as users', 'fmrs.user_id = users.id', 'LEFT');
$query->where('section_id',$this->ci->session->userdata('section-fmr'));
//}
$this->ci->db->stop_cache();
$datatable = new Datatables($this->columnOrder,$this->columnSearch,$query);
$result = [
'list' => $datatable->getDatatables(),
'countAll' => $datatable->countAll(),
'countFiltered' => $datatable->countFiltered()
];
$this->ci->db->flush_cache();
return $result;
}
And probably use num_rows() instead of num_rows here, num_rows gave me a NULL instead of a count:
Datatables
/**
* Count filtered rows
*
* #return integer
*/
public function countFiltered()
{
$query = $this->query->get();
return $query->num_rows();
}
What is the best practice to chain repository methods to reuse query building logic?
Here is how I did it, but I doubt if this is the right way:
use Doctrine\ORM\Mapping;
use Doctrine\ORM\EntityManager;
class OrderRepository extends \Doctrine\ORM\EntityRepository
{
private $q;
public function __construct(EntityManager $em, Mapping\ClassMetadata $class)
{
parent::__construct($em, $class);
$this->q = $this->createQueryBuilder('o');
}
public function getOneResult()
{
return $this->q->getQuery()->getOneOrNullResult();
}
public function getResult()
{
return $this->q->getQuery()->getResult();
}
public function filterByStatus($status)
{
$this->q->andWhere('o.status = :status')->setParameter('status', $status);
return $this;
}
public function findNextForPackaging()
{
$this->q->leftjoin('o.orderProducts', 'p')
->orderBy('o.deliveryDate', 'ASC')
->andHaving('SUM(p.qtePacked) < SUM(p.qte)')
->groupBy('o.id')
->setMaxResults(1);
return $this;
}
}
This allows me to chain method like this:
$order = $em->getRepository('AppBundle:Order')->filterByStatus(10)->findNextForPackaging()->getOneResult();
This is of course just an example. In reality there are many more methods that can be chained.
One big problem with this is the fact that I need a join for some of the "filters", so I have to check if the join has already been set by some method/filter before I add it. ( I did not put it in the example, but I figured it out, but it is not very pretty )
The other problem is that I have to be careful when using the repository, as the query could already be set to something, so I would need to reset the query every time before using it.
I also understand that I could use the doctrine "matching" method with criteria, but as far as I understood, this is rather expensive, and also, I don't know how to solve the "join" Problem with that approach.
Any thoughts?
I made something similar to what you want:
Controller, this is how you use it. I am not returning Response instance but serialize the array in kernel.view listener but it is still valid example:
/**
* #Route("/root/pending_posts", name="root_pending_posts")
* #Method("GET")
*
* #return Post[]
*/
public function pendingPostsAction(PostRepository $postRepository, ?UserInterface $user): array
{
if (!$user) {
return [];
}
return $postRepository->begin()
->wherePublished(false)
->whereCreator($user)
->getResults();
}
PostRepository:
class PostRepository extends BaseRepository
{
public function whereCreator(User $user)
{
$this->qb()->andWhere('o.creator = :creator')->setParameter('creator', $user);
return $this;
}
public function leftJoinRecentComments(): self
{
$this->qb()
->leftJoin('o.recentCommentsReference', 'ref')->addSelect('ref')
->leftJoin('ref.comment', 'c')->addSelect('c');
return $this;
}
public function andAfter(\DateTime $after)
{
$this->qb()->andWhere('o.createdAt > :after')->setParameter('after', $after);
return $this;
}
public function andBefore(\DateTime $before)
{
$this->qb()->andWhere('o.createdAt < :before')->setParameter('before', $before);
return $this;
}
public function wherePublished(bool $bool)
{
$this->qb()->andWhere('o.isPending = :is_pending')->setParameter('is_pending', !$bool);
return $this;
}
}
and BaseRepository has most used stuff, still work in progress:
namespace wjb\CoreBundle\Model;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
abstract class BaseRepository extends EntityRepository
{
/** #var QueryBuilder */
private $qb;
public function begin()
{
$this->qb = $this->createQueryBuilder('o');
return $this;
}
public function qb(): QueryBuilder
{
return $this->qb;
}
public function none()
{
$this->qb()->where('o.id IS NULL');
return $this;
}
public function setMaxResults($maxResults)
{
$this->qb()->setMaxResults($maxResults);
return $this;
}
public function addOrderBy($sort, $order = null)
{
$this->qb()->addOrderBy($sort, $order);
return $this;
}
public function getResults()
{
return $this->qb()->getQuery()->getResult();
}
}
This helps me a lot in chaining calls like in controller example.
I need to check that {subcategory} has parent category {category}. How i can get the model of {category} in second binding?
I tried $route->getParameter('category');. Laravel throws FatalErrorException with message "Maximum function nesting level of '100' reached, aborting!".
Route::bind('category', function ($value) {
$category = Category::where('title', $value)->first();
if (!$category || $category->parent_id) {
App::abort(404);
}
return $category;
});
Route::bind('subcategory', function ($value, $route) {
if ($value) {
$category = Category::where('title', $value)->first();
if ($category) {
return $category;
}
App::abort(404);
}
});
Route::get('{category}/{subcategory?}', 'CategoriesController#get');
Update:
Now i made this, but i think it's not the best solution.
Route::bind('category', function ($value) {
$category = Category::where('title', $value)->whereNull('parent_id')->first();
if (!$category) {
App::abort(404);
}
Route::bind('subcategory', function ($value, $route) use ($category) {
if ($value) {
$subcategory = Category::where('title', $value)->where('parent_id', $category->id)->first();
if (!$subcategory) {
App::abort(404);
}
return $subcategory;
}
});
return $category;
});
You may try this and should work, code is self explanatory (ask if need an explanation):
Route::bind('category', function ($value) {
$category = Category::where('title', $value)->first();
if (!$category || $category->parent_id) App::abort(404);
return $category;
});
Route::bind('subcategory', function ($value, $route) {
$category = $route->parameter('category');
$subcategory = Category::where('title', $value)->whereParentId($category->id);
return $subcategory->first() ?: App::abort(404); // shortcut of if
});
Route::get('{category}/{subcategory?}', 'CategoriesController#get');
Update: (As OP claimed that, there is no parameter method available in Route class):
/**
* Get a given parameter from the route.
*
* #param string $name
* #param mixed $default
* #return string
*/
public function getParameter($name, $default = null)
{
return $this->parameter($name, $default);
}
/**
* Get a given parameter from the route.
*
* #param string $name
* #param mixed $default
* #return string
*/
public function parameter($name, $default = null)
{
return array_get($this->parameters(), $name) ?: $default;
}
I can't test this right now for you, but the closure function receives a $value and $route.
The last one is a instance of '\Illuminate\Routing\Route' (http://laravel.com/api/class-Illuminate.Routing.Route.html), and perhaps you could use the getParameter() method to retrieve some data....
I recently ran into same issues while trying to auto validate my stories existence inside my Session Model. So, i tried to check my Story model existence inside my Session Model using model bindings.
This is my solution
$router->bind('stories', function($value, $route) {
$routeParameters = $route->parameters();
$story = Story::where('id', $value)->where('poker_session_id', $routeParameters['poker_sessions']->id)->first();
if (!$story) {
throw new NotFoundHttpException;
}
return $story;
});
You can actually get route parameters using $route->parameters(), which returns an array. In my case, "poker_sessions" key contain an PokerSession Model, which i want.
Please be careful and use this only when you have a /category/{category}/subcategory/{subcategory} url like. Not subcategory without any {category}.
Good luck!.
I hope I can explain this clearly, apologies in advance if it is confusing. I have a goals table which hasOne of each of bodyGoalDescs, strengthGoalDescs and distanceGoalDescs as shown below
goals.php
class Goal extends BaseModel
{
protected $guarded = array();
public static $rules = array();
//define relationships
public function user()
{
return $this->belongsTo('User', 'id', 'userId');
}
public function goalStatus()
{
return $this->hasOne('GoalStatus', 'id', 'goalStatus');
}
public function bodyGoalDesc()
{
return $this->hasOne('BodyGoalDesc', 'id', 'bodyGoalId');
}
public function distanceGoalDesc()
{
return $this->hasOne('DistanceGoalDesc', 'id', 'distanceGoalId');
}
public function strengthGoalDesc()
{
return $this->hasOne('StrengthGoalDesc', 'id', 'strengthGoalId');
}
//goal specific functions
public static function yourGoals()
{
return static::where('userId', '=', Auth::user()->id)->paginate();
}
}
each of the three tables looks like this with the function details changed
class BodyGoalDesc extends BaseModel
{
protected $guarded = array();
public static $rules = array();
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'bodyGoalDescs';
//define relationships
public function goal()
{
return $this->belongsTo('Goal', 'bodyGoalId', 'id');
}
}
a goal has either a body goal, a strength goal, or a distance goal. I am having a problem with this method in the controller function
<?php
class GoalsController extends BaseController
{
protected $goal;
public function __construct(Goal $goal)
{
$this->goal = $goal;
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
$thisgoal = $this->goal->find($id);
foreach ($this->goal->with('distanceGoalDesc')->get() as $distancegoaldesc) {
dd($distancegoaldesc->DistanceGoalDesc);
}
}
}
when I pass through goal 1 which has a distance goal the above method dies and dumps the Goal object with the details of goal 1 and an array of its relations including an object with DistanceGoalDes.
when I pass through goal 2 it passes through exactly the same as if I had passed through goal 1
if I dd() $thisgoal i get the goal that was passed through
what I want ultimately is a method that returns the goal object with its relevant goal description object to the view but this wont even show me the correct goal details not too mind with the correct relations
this function is now doing what I want it to do, I am sure there is a better way (besides the fact that its happening in the controller right now) and I would love to hear it.
public function show($id)
{
$thisgoal = $this->goal->find($id);
if (!$thisgoal->bodyGoalDesc == null) {
$goaldesc = $thisgoal->bodyGoalDesc;
return View::make('goals.show')
->with('goal', $thisgoal)
->with('bodygoaldesc', $goaldesc);
} elseif (!$thisgoal->strengthGoalDesc == null) {
$goaldesc = $thisgoal->strengthGoalDesc;
return View::make('goals.show')
->with('goal', $thisgoal)
->with('strengthgoaldesc', $goaldesc);
} elseif (!$thisgoal->distanceGoalDesc == null) {
$goaldesc = $thisgoal->distanceGoalDesc;
return View::make('goals.show')
->with('goal', $thisgoal)
->with('distancegoaldesc', $goaldesc);
}
}
I'm trying to include unit-testing in a new Laravel app I'm building.
Right now I'm want to test my OrderController. The index method of this controller looks like this:
public function index()
{
// We need the extra 'orders()' for the query scope
$orders = $this->order->orders()->paginate($this->perPage);
$this->layout->content = View::make('orders.index', compact('orders'));
}
Now I have my test that looks like this:
public function testIndex()
{
$this->call('GET', 'orders');
$this->assertResponseOk();
$this->assertViewHas('orders');
}
Now if I run phpunit, the test does run, but I'm getting: 1) OrderControllerTest::testIndex
Failed asserting that an array has the key 'orders'.
I've tracked down the issue to using Controller Layouts $this->layout.
If I just do return View::make() the test does pass, if I return $this->layout... it also does pass, but this destroys the actual app.
So only option I've found is to use return View::make() and have #extends('master') in that view. But it's seems strange to me that you can't use Controller Layouts in your app if you want to test it.
Am I doing something wrong?
Edit:
I had the same problem and here is slightly messy solution that works!
View::composer('ordersviewname', function($view) {
$this->assertArrayHasKey('orders', $view->getData());
});
$this->call('GET', 'orders');
Note that you have to put this code BEFORE $this->call()
EDIT:
Here is more elegant solution!
Add functions to TestCase
protected $nestedViewData = array();
public function registerNestedView($view)
{
View::composer($view, function($view){
$this->nestedViewsData[$view->getName()] = $view->getData();
});
}
/**
* Assert that the given view has a given piece of bound data.
*
* #param string|array $key
* #param mixed $value
* #return void
*/
public function assertNestedViewHas($view, $key, $value = null)
{
if (is_array($key)) return $this->assertNestedViewHasAll($view, $key);
if ( ! isset($this->nestedViewsData[$view]))
{
return $this->assertTrue(false, 'The view was not called.');
}
$data = $this->nestedViewsData[$view];
if (is_null($value))
{
$this->assertArrayHasKey($key, $data);
}
else
{
if(isset($data[$key]))
$this->assertEquals($value, $data[$key]);
else
return $this->assertTrue(false, 'The View has no bound data with this key.');
}
}
/**
* Assert that the view has a given list of bound data.
*
* #param array $bindings
* #return void
*/
public function assertNestedViewHasAll($view, array $bindings)
{
foreach ($bindings as $key => $value)
{
if (is_int($key))
{
$this->assertNestedViewHas($view, $value);
}
else
{
$this->assertNestedViewHas($view, $key, $value);
}
}
}
public function assertNestedView($view)
{
$this->assertArrayHasKey($view, $this->nestedViewsData);
}
Now you would rewrite your test
$view='orders';
$this->registerNestedView($view);
$this->call('GET', 'orders');
$this->assertNestedViewHas($view, 'orders');
assertNestedViewHas() has all the same feautures as assertViewHas() !
I added another function assertNestedView() which just check if given view was called.