cakephp: Acl problem: not able to populate aros_acos table with initdb function in users controller - cakephp

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.

Related

Get multiple values from a function in yii2

I have a function in my Model which is passed a single value and in which I have a query which is giving me two results.
public static function updImgtbl($id)
{
$total_images = 0;
$images_uploaded = 0;
$sqlquery = "SELECT SUM(CASE WHEN ims.`installation_id` = $id THEN 1 ELSE 0 END) AS total_images,
SUM(CASE WHEN ims.`image_upload_flag` = 1 AND ims.`installation_id` = $id THEN 1 ELSE 0 END) AS images_uploaded
FROM `installation_images_site` ims
INNER JOIN `installations` ins ON ins.`id` = ims.`installation_id`
WHERE (ims.`image_upload_flag` = 1 AND ims.`installation_id` = $id)
OR (ims.`installation_id` = $id)
GROUP BY ims.`installation_id`";
$pt = Yii::$app->db->createCommand($sqlquery)->queryAll();
foreach ($pt as $val)
{
$total_images= $val['total_images'];
$images_uploaded = $val['images_uploaded'];
}
return ["total_images" => $total_images, "images_uploaded"=>$images_uploaded];
}
This function is called from an API
$response = Installations::updImgtbl($install_id);
print_r($response);
exit();
The response is below
Array
(
[total_images] => 4
[images_uploaded] => 1
)
But I want to get these values in a variable in my API. How can I achieve it?
You can simply:
list($total_images, $images_uploaded) = array_values($response);
Then, you have total_images value in $total_images variable and images_uploaded in $images_uploaded variable.
I mean:
list($total_images, $images_uploaded) = Installations::updImgtbl($install_id);
echo $total_images; // 4
echo $images_uploaded; // 1
You can read more about list in official doc.
Another way is using extract() function. For example:
$response = Installations::updImgtbl($install_id);
extract($response);
echo $total_images; // 4
echo $images_uploaded; // 1

node_load_multiple() can it go through the array?

I'm trying to do this to get the "floor" value of my node :
$type = "desk";
$floor = '2';
$nodes = node_load_multiple(array(), array('field_floor["und"][0]["value"]' => $floor, 'type' => $type));
I can get all my desk if I'm just doing $nodes = node_load_multiple(array(), array('type' => $type)); and I can find a deck with for exemple the title, but is it possible to go through the arrays to get the 'value' and check it in the query ?
Thank you for your answers.
To get value of a field you have multiple methods :
Field get item:
$nid = 2;
$node = node_load($nid);
$floor = field_get_items($node , 'node', 'field_floor');
$floor = reset($floor); // or loop on it, here take first value if multiple
echo $floor['value'];
https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_get_items/7.x
Entity metadata wrapper:
$nid = 2;
$node = node_load($nid);
$wrapper = entity_metadata_wrapper('node', $node);
$floor = $wrapper->field_floor->value();
echo $floor;
https://www.drupal.org/docs/7/api/entity-api/entity-metadata-wrappers
Direct way :
global $language; // take current language
$nid = 2;
$node = node_load($nid);
echo $node->field_floor[$language->language][0]['value'];

Cakephp pagination for a custom function

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 ?

Cloud Datastore API - php - GqlQuery - Binding args

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.

Symfony2: Querying for object with 2 other objects as parameters

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
));

Resources