I have a function in my model EmployeePersonal.php named get_employee_details which accepts some parameter and return the employees details. This function is used my many controllers and I prefer not to change this function.
Now I have a function view in my EmployeePersonalsController.php which outputs approx 4000 results. eg
$res = $this->EmployeePersonal->get_employee_details(
$office_id = $office_id,
$epf_no = $epf_no,
$employee_personal_id = $employee_personal_id,
$status = $status,
$permanent_district_id = $permanent_district_id,
$present_district_id = $present_district_id,
$department_id = $department_id,
$designation_id = $designation_id,
$category_id = $category_id,
$gender_type = $gender_type,
$offset = $offset,
$size = $size
);
I need to put a pagination in my view function , is there any way how can I put the $paginate here ?
Related
Using DD in my controller i have this result. Now i want to get the "id":45 in column name data
Controller:
public function readbyid($id){
$test = DB::table('notifications')->where('id','=', $id)->get();
dd($test);}
Id is unique, use first instead of get
$test = DB::table('notifications')->where('id', $id)->first();
$dataId = $test->data->id;
//or $dataId = json_decode($test->data)->id;
use App\Notification;
$test = Notification::where('id', $id)->first();
$dataId = $test->id;
dd($dataId);
I'm trying to bind some arguments to my GQL query string.
Everything's fine when I run the query without binding anything:
$result = $DB->query_entities("SELECT * FROM kind WHERE fieldName = 4
LIMIT 1");
But I don't know how to bind some arguments. This is how I'm trying to do so:
function query_entities($query_string, $args=[]){
$gql_query = new Google_Service_Datastore_GqlQuery();
$gql_query->setQueryString($query_string);
$gql_query->setAllowLiteral(true);
if( !empty($args) ){
$binding_args = [];
foreach ($args as $key => $value) {
$binding_value = new Google_Service_Datastore_Value();
$binding_value->setIntegerValue($value);
$arg = new Google_Service_Datastore_GqlQueryArg();
$arg->setName($key);
$arg->setValue($binding_value);
$binding_args[] = $arg;
}
$gql_query->setNameArgs($binding_args);
}
$req = new Google_Service_Datastore_RunQueryRequest();
$req->setGqlQuery($gql_query);
return $this->dataset->runQuery($this->dataset_id, $req, []);
}
$exampleValue = 4;
$result = $DB->query_entities("SELECT * FROM kind WHERE fieldName = :fieldName LIMIT 1", ["fieldName" => $exampleValue]);
Or:
function query_entities($query_string, $args=[]){
$gql_query = new Google_Service_Datastore_GqlQuery();
$gql_query->setQueryString($query_string);
$gql_query->setAllowLiteral(true);
if( !empty($args) ){
$binding_args = [];
foreach ($args as $value) {
$binding_value = new Google_Service_Datastore_Value();
$binding_value->setIntegerValue($value);
$arg = new Google_Service_Datastore_GqlQueryArg();
$arg->setValue($binding_value);
$binding_args[] = $arg;
}
$gql_query->setNumberArgs($binding_args);
}
$req = new Google_Service_Datastore_RunQueryRequest();
$req->setGqlQuery($gql_query);
return $this->dataset->runQuery($this->dataset_id, $req, []);
}
$exampleValue = 4;
$result = $DB->query_entities("SELECT * FROM kind WHERE fieldName = :1 LIMIT 1", [$exampleValue]);
This is what I get:
'Error calling POST https://www.googleapis.com/datastore/v1beta2/datasets/age-of-deployment/runQuery: (400) Lexical error at line 1, column 52. Encountered: ":" (58), after : ""' in ...
Cloud Datastore GQL and Python GQL (App Engine) handle argument binding slightly differently.
In this case, you need to use a # instead of a :, for instance:
SELECT * FROM kind WHERE fieldName = #fieldName LIMIT 1
or
SELECT * FROM kind WHERE fieldName = #1 LIMIT 1
Here's the reference documentation for argument binding in Cloud Datastore GQL.
What is the best way to do the following: I have a gameobject entity which has properties game and user. Now my method gets the game id and the user id and i want to look for the gameobject with game = object of game id and user = object of user id.
I have tried the following:
$game = $this->getDoctrine()->getRepository("xxx:Game")->find($game_id);
$user = $this->getDoctrine()->getRepository("xxx:User")->find($user_id);
$gameobject_query = $em->getRepository('xxx:Gameobject')->createQueryBuilder('g')
->where('g.game = :game AND g.user = :user')
->setParameters(array(
'game' => $game,
'user' => $user
))
->getQuery();
$gameobject = $gameobject_query->getResult();
Your advices would be appriciated :)
I think that something like the following should work:
$repository = this->getDoctrine()->getRepository('xxx:GameObject');
$queryBuilder = $repository->createQueryBuilder('go')
->innerJoin('go.game', 'g')
->innerJoin('go.user', 'u')
->where('g.id = :gameId')
->andWhere('u.id = :userId')
->setParameter('gameId', $gameId)
->setParameter('userId', $userId);
$gameObjects = $queryBuilder->getQuery()
->execute();
Alternatively, the following may also work and be more efficient: It should do the same but without needing to join to the other entities:
$repository = this->getDoctrine()->getRepository('xxx:GameObject');
$queryBuilder = $repository->createQueryBuilder('go')
->where('IDENTIY(go.game) = :gameId')
->andWhere('IDENTITY(go.user) = :userId')
->setParameter('gameId', $gameId)
->setParameter('userId', $userId);
$gameObjects = $queryBuilder->getQuery()
->execute();
Why didn't you use the findOneBy method of your Gameobject repo ?
$game = $this->getDoctrine()->getRepository("xxx:Game")->find($game_id);
$user = $this->getDoctrine()->getRepository("xxx:User")->find($user_id);
$gameobject = $em->getRepository('xxx:Gameobject')->findOneBy(array(
'game' => $game,
'user' => $user
));
I don't know what exactly is happening with my CakePHP app. It worked last week and I have literally changed nothing with that particular file.
When I use find 'all' in my controller it dumps a semi-colon in my view even if there is nothing in the view file.
Here is my code
$evts = $this->Event->find('all');
There is nothing in my view file. I don't know if it makes a difference but I'm using a json view.
As requested here is the complete code for the action
public function search(){
$this->Event->recursive = 2;
$conditions = array();
if(!empty($this->request->query['name'])){
$conditions = array('Event.name LIKE ' => "%" . str_replace(" ","%", $this->request->query['name']) . "%");
}
if(!empty($this->request->query['home'])){
$conditions = array('Event.home_team_id' => $this->request->query['home']);
}
if(!empty($this->request->query['away'])){
$conditions = array('Event.away_team_id' => $this->request->query['away']);
}
$limit = 25;
if(!empty($this->request->query['limit'])){
$limit = $this->request->query['limit'];
}
//$evts = $this->Event->find('all',array('conditions'=>array($conditions),'order' => array('Event.start_time'),'limit'=>$limit));
$evts = $this->Event->find('all');
$this->set('events',$evts);
}
Everything in the view has been commented out ... but here is the code anyway
$results = array();
$i = 0;
foreach ($events as $event) {
$results[$i]['id'] = $event['Event']['id'];
$results[$i]['label'] = $event['Event']['name'] . "(" . date_format(date_create($event['Event']['start_time']), 'D, jS M Y') . ")";
$results[$i]['value'] = $event['Event']['name'];
$results[$i]['home_team_name'] = $event['HomeTeam']['name'];
$results[$i]['away_team_name'] = $event['AwayTeam']['name'];
$results[$i]['sport_name'] = $event['Sport']['name'];
$results[$i]['tournament_name'] = $event['Tournament']['name'];
$results[$i]['start_time'] = $event['Event']['start_time'];
$results[$i]['img'] = $event['Event']['img_path'];
$results[$i]['listener_count'] = 0; //TODO Get the follower count
$i++;
}
echo json_encode($results);
Display
If you changed nothing with that particular file then perhaps you have installed callbacks (either in the controller or in the model) that echo that ";". Look for 'afterFind' calls...
Search your model and your controller folders for "echo statement". In fact you should search your entire app folder except for the Views folder.
When I try to run app/users/initdb, I'm getting warning 512 and i'm not able to populate the aros_acos table. Does anyone know on i'm doing wrong:
Warning (512): DbAcl::allow() - Invalid node [CORE\cake\libs\controller\components\acl.php, line 361]Code | Context
if ($perms == false) {
trigger_error(__('DbAcl::allow() - Invalid node', true), E_USER_WARNING);$aro = Group
Group::$name = "Group"
Group::$validate = array
Group::$hasMany = array
Group::$actsAs = array
Group::$useDbConfig = "default"
Group::$useTable = "groups"
Group::$displayField = "name"
Group::$id = 7
Group::$data = array
Group::$table = "groups"
Group::$primaryKey = "id"
Group::$_schema = array
Group::$validationErrors = array
Group::$tablePrefix = ""
Group::$alias = "Group"
Group::$tableToModel = array
Group::$logTransactions = false
Group::$cacheQueries = false
Group::$belongsTo = array
Group::$hasOne = array
Group::$hasAndBelongsToMany = array
Group::$Behaviors = BehaviorCollection object
Group::$whitelist = array
Group::$cacheSources = true
Group::$findQueryType = NULL
Group::$recursive = 1
Group::$order = NULL
Group::$virtualFields = array
Group::$__associationKeys = array
Group::$__associations = array
Group::$__backAssociation = array
Group::$__insertID = NULL
Group::$__numRows = NULL
Group::$__affectedRows = NULL
Group::$_findMethods = array
Group::$User = User object
Group::$Aro = Aro object
$aco = "controllers"
$actions = "*"
$value = 1
$perms = false
$permKeys = array(
"_create",
"_read",
"_update",
"_delete"
)
$save = array()DbAcl::allow() - CORE\cake\libs\controller\components\acl.php, line 361
AclComponent::allow() - CORE\cake\libs\controller\components\acl.php, line 106
UsersController::initdb() - APP\controllers\users_controller.php, line 82
Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 204
Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 171
[main] - APP\webroot\index.php, line 83
The following is my users_controller.php
class UsersController extends appController{
var $name = 'Users';
function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow(array('*'));
}
function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
function login(){
}
function logout(){
}
.........
function initdb(){
//gets reference of Group model and modify its id to be able to specify the ARO we wanted, this is due to how AclBehavior works. AclBehavior does not set the alias field in the aros table so we must use an object reference or an array to reference the ARO we want.
$group=& $this->User->Group;
//Allow admins to everything
$group->id=7;
$this->Acl->allow($group,'controllers');
//allow managers to posts and widgets
$group->id=8;
$this->Acl->deny($group,'controllers');
$this->Acl->allow($group,'controllers/Posts');
$this->Acl->allow($group, 'controllers/Widgets');
//allow users to only add and edit on posts and widgets
$group->id=9;
$this->Acl->deny($group,'controllers');
$this->Acl->allow($group,'controllers/Posts/add');
$this->Acl->allow($group,'controllers/Posts/edit');
$this->Acl->allow($group,'controllers/Widgets/add');
$this->Acl->allow($group,'controllers/Widgets/edit');
//we add an exit to avoid an ugly "missing views" error message
echo "all done";
exit;
}
any help is much appreciated. Thanks.