I have this code:
$userObj = ClassRegistry::init('User');
$userObj->contain();
$conditions = "User.studio_id = '".$studioID."' AND User.usergroup_id = 5";
$studioAdmin = $userObj->find($conditions);
The one that is causing the error is this line:
$studioAdmin = $userObj->find($conditions);
When I say error, it does not print anything or any warning of error, it just stops the code below it, I noticed that one because when I try to echo a code above it, it prints it, but when I try to echo a code below it, it does not print anything,
What is the problem here. Your help will be greatly appreciated! Thanks! :)
The better practice way of loading models in components is to go via the controller, and use loadModel()
In your component, set up the initialize()
function initialize($controller, $settings) {
$this->Controller =& $controller;
}
Then in your component function, use loadModel to load the model
$this->Controller->loadModel('Modelname');
$this->Modelname->save($data);
and also for find condition
$users = $this->Modelname->find('all', array(
'conditions' => array(
'User.studio_id' => $studioID,
'User.usergroup_id' => 5
)
));
You should be doing this:
$studioAdmin = $userObj->find('all', array( 'conditions' => $conditions ) );
Do you have PHP error messaging turned on? Did you check your logs to see what the specific error is?
Also, by cake standards, it is better to build your conditions clause this way:
$conditions = array(
"User.studio_id" => $studioID,
"User.usergroup_id" => 5"
);
Related
I'm using Codeigniter 4.
And inserting new data like this,
$data = [
'username' => 'darth',
'email' => 'd.vader#theempire.com'
];
$userModel->save($data);
Which is mentioned here: CodeIgniter’s Model reference
It's doing the insertion.
But I haven't found any reference about to get the inserted id after insertion.
Please help! Thanks in advance.
This also works.
$user= new UserModel();
$data = [
'username' => 'darth',
'email' => 'd.vader#theempire.com'
];
$user->insert($data);
$user_id = $user->getInsertID();
I got a simple solution after researching on the core of the CI 4 framework.
$db = db_connect('default');
$builder = $db->table('myTable');
$data = [
'username' => 'darth',
'email' => 'd.vader#theempire.com'
];
$builder->insert($data);
echo $db->insertID();
Hope they'll add a clear description on the docs soon.
There are three way to get the ID in ci4:
$db = \Config\Database::connect();
$workModel = model('App\Models\WorkModel', true, $db);
$id = $workModel->insert($data);
echo $id;
echo '<br/>';
echo $workModel->insertID();
echo '<br/>';
echo $db->insertID();
In fact, what you did is correct.
You did it in the best and easiest way and following the Codeigniter 4 Model usage guide.
You just missed: $id = $userModel->insertID;
Complete code using your example:
$data = [
'username' => 'darth',
'email' => 'd.vader#theempire.com'
];
$userModel->save($data);
$id = $userModel->insertID;
That's it. You don't need all this code from the examples above nor calling database service or db builder if you're using codeigniter's models.
Tested on CodeIgniter 4.1.1 on 3/19/2021
To overcome this, I modified system/Model.php in the save() method---
$response = $this->insert($data, false);
// add after the insert() call
$this->primaryKey = $this->db->insertID();
Now, in your models, you can just reference "$this->primaryKey" and it will give you the needed info, while maintaining the data modeling functionality.
I'm going to submit this over to the CI developers, hopefully it will be added in.
For CI4
$settings = new SettingsModel();
$settingsData = $settings->find(1);
<?php namespace App\Models;
use App\Models\BaseModel;
class SettingsModel extends BaseModel
{
protected $table = 'users';
protected $primaryKey = 'id';
}
$settings->find(1); will return a single row. it will find the value provided as the $primaryKey.
hi guys in my case i use ci model to save data and my code is :
$x=new X();
$is_insert= $x->save(['name'=>'test','type'=>'ss']);
if($is_insert)
$inserted_id=$x->getInsertID()
I'm using mysql for my database then I ran this inside my seeder
$university = $this->db->table('universities')->insert([
'name' => 'Harvard University'
]);
$faculty = $this->db->table('faculties')->insert([
'name' => 'Arts & Sciences',
'university' => $university->resultID
]);
Look at code line 6
$university->resultID
variable $university here is type object of CodeIgniter\Database\MySQLi\Result class
Corect me if I'm wrong or any room for improvements
I had the same problem but, unfortunately, the CI4 documentation doesn't help much. The solution using a builder woks, but it's a workaround the data modeling. I believe you want a pure model solution, otherwise you wouldn't be asking.
$data = [
'username' => 'darth',
'email' => 'd.vader#theempire.com'
];
$id = $userModel->save($data);
Trying everything I could think of I decided to store the result of the save method to see if returned a boolean value to indicate if the saving was sucessful. Inspecting the variable I realized it returns exactly what I wanted: the lost insertID.
I believe CodeIgniter 4 is quite an easy and capable framework that does a decent job in shared hosts where other frameworks can be a little demanding if you're learning but lacks the same fantastic documentation and examples of CI3. Hopefully, that's only temporary.
By the way, you code works only if you are using the $userModel outside the model itself, for example, from a Controller. You need to create a model object like:
$userModel = New WhateverNameModel();
$data = [any data];
$userModel->save($data);
Alternatively, if you are programming a method inside the model itself (my favorite way), you should write
$this->save($data);
I'm trying to wrap my head around permissions and arrays in Sentry. I've got them working when I hardcode them, that's easy, but assigning them from a form is proving to be very... Frustrating to say the least. Here is the function that handles it:
public function postGroup(){
$name = Input::get('name');
$inputs = Input::except('name');
$permissions = array("self.view" => 1, "self.update" => 1);
foreach($inputs as $key => $value)
{
$raw_name = $key;
$name = str_replace('_', '.', $raw_name);
array_push($permissions, array($name => 1)); // The issue is here.
}
...
Basically, we now have an array $permissions with [user.create] => 1, [user.view] => 1 etc etc for each permission passed from the Form. Next, using Sentry::createGroup I need to set the permissions based on this array.
//Create the group
$group = Sentry::createGroup(array(
'name' => $name,
'permissions' => $permissions //Doesn't work.
));
When I check my database, I see this:
{"self.view":1,"self.update":1,"0":1,"1":1}
It should be something like this:
{"self.view":1,"self.update":1,"user.create":1,"user.view":1}
So we can see the self.* ones work fine, but since I'm calling array_push($permissions, array($name => 1)); I'm making $permissions a multidimensional array (Hence why we see "0":1 in the database; it's looking at the index. But I'm not 100% sure how to fix this...
So, any insight into array handling and Sentry Permissions would be helpful. I'll keep trying in the meantime.
Thanks in advance!
I'm an idiot... This is a really simple answer. Replace:
array_push($permissions, array($name => 1)); // The issue is here.
with (and I can't believe how simple this is)
$permissions[$name] = 1;
to avoid the multidimensional array issue. Le Sigh.
i've been trying to learn more about how to have fat models and skinny controllers the right way, because before my models would have basically no code and i'm trying to change that. My function works, but now i'm trying to combine two find() queries that look almost exactly the same except one of them has a simple condition.
My model looks something like this:
function pieChart() {
//Get Data for PieChart
$this->RecordDrug->virtualFields['sum'] ='COUNT(*)';
$records = array();
$records=$this->RecordDrug->find('list',
array('fields' => array( 'Drug.drug', 'sum'),
'contain' => array( 'Drug', 'Record' ),
'group' => 'Drug.Drug'
));
$this->set('output',$records);
return $records;
}
I will have two controllers using this. One of them will use this code as is, just simply call the pieChart() function. The other controller will have to see a condition that only selects the users entries. So
'conditions' => array('Record.user_id' => $this->Auth->user('id'))
How do I go about this the right way? I think i'm having trouble with this because my OOP knowledge is pretty limited. If anyone has any examples or resources that can help me make my find() functions more efficient and streamlined, i'd really appreciate it.
I done that kind of things very simple:
public function myQuery($conditions = null) {
$this->virtualFields['sum'] ='COUNT(*)';
$result = $this->find('all', array('conditions' => $conditions,
'fields' => array('Drug.drug', 'sum'),
'contain' => array('Drug','Record'),
'group' => 'Drug.Drug'
));
return $result;
}
Now you can call this with your argument:
$conditions = array('Record.user_id' => $this->Auth->user('id'));
$data = $this->RecordDrug->myQuery($conditions);
Or without it:
$data = $this->RecordDrug->myQuery();
Note that in this case you need to put myQuery() in to RecordDrug model and you need to use 'all' instead of 'list', because 'list' doesn't support contain option.
So now if you have additional conditions - you just need to pass it in the argument. If you leave it null - it do the query without the conditions statement.
Am using jsTree + PHP, and I would like to load up two different trees from two different database tables, depending on what user type the logged in user is.
I tracked down the code to the "class.tree.php" file,
class json_tree extends _tree_struct {
function __construct($table = 'tree', $fields = array(), $add_fields = array("title" => "title", "type" => "type")) {
parent::__construct($table, $fields);
$this->fields = array_merge($this->fields, $add_fields);
$this->add_fields = $add_fields;
}
Where the $table = 'tree' is hard-coded. I tried removing it (so just $table), and passing 'tree' through as
$jstree = new json_tree('tree');
but that didn't work. Any ideas/help would be greatly appreciated!
To those curious, I had completely forgotten that server.php also includes a jstree (I had also initiated it in my left menu). For those interested, or who need a conditional based tree, what I did was:
removed the $table = 'tree' in the __construct as above, which becomes
function __construct($table, $fields = array(), $add_fields = array("title" => "title", "type" => "type"))
added a session to the config.php (session_start())
I have a log in, which allows me to get the condition
in server.php, I did the following
if($_SESSION['condition'] == 1) { $jstree = new json_tree("tree");}
else { $jstree = new json_tree("tree_users");}
I hope this helps someone!
I am trying to extract ONLY the PlanDetails where PlanDetail.company_id = Company.id AND PlanDetail.id' => $id.. ( you can see the conditions in my controller below)..
Controller:
function pd_list_by_company($id = null) {
$this->recursive = 2; // I am going to use containable to trim this.
return $this->PlanDetail->find('all',
array('conditions' =>
array('AND' =>
array('PlanDetail.company_id' => 'Company.id',
array('PlanDetail.id' => $id)))));
}
Test View:
$planDetailsByCompany = $this->requestAction('/planDetails/pd_list_by_company');
debug($planDetailsByCompany );
Output result of my debug??
Array()
If I remove the conditions and just have the find all, I get all PlanDetails as expected, so I know the data is being passed.. SQL debug dump even shows the query:
WHERE ((`PlanDetail`.`company_id` = 'Company.id') AND (`PlanDetail`.`id` IS NULL))
And yes, I did notice the $id is NULL, and I know the value needs to be there.. So maybe my question is why is the $id value not being passed to the controller even though I can see the PlanDetail.id value on a find('all') w/ out the conditions??
Thanks for any tips.
Since $id seems to be null, I would assume that you call the function without the parameter. And you don't get an error message, because as far as PHP is concerned the parameter is optional. In this case it's clearly required, so you should make it a required parameter in your function declaration:
function pd_list_by_company($id) {
Also you could simplify the return statement, you do not need the AND:
return $this->PlanDetail->find('all',
array('conditions' =>
array('PlanDetail.company_id' => 'Company.id','PlanDetail.id' => $id)
)
);
To answer the question why is the $id not being passed is because you're not passing it
To pass say $id of 2 you need to do the following in your requestAction
$this->requestAction('/planDetails/pd_list_by_company/2');
Seems to me that your code should just be
return $this->PlanDetail->find('array('PlanDetail.id' => $id));
Assuming you have the $this->PlanDetail->recursive flag set to > 0, your Model should already know about and return the associated data for any 'Company' table.....
I'm used to an old (1.3) version of CakePHP but the find() function is pretty basic and is designed to only return one row.
and yes, you definitely need to call the function with the id appended to the url, eg.
$planDetailsByCompany = $this->requestAction('/planDetails/pd_list_by_company/999');