i am trying to SAVE data of Sizes which is coming from with multiple same name input.
Problem is i am not able to save data. Only last value "sizes" is saving in database.
if ($this->EquipmentType->save($this->request->data['EquipmentType'], false)) {
$id = $this->EquipmentType->getLastInsertId();
$this->loadModel('EquipmentTypesSize');
$sizesArray = $this->request->data['EquipmentType']['size'];
foreach($sizesArray as $val){
$data[] = array('EquipmentTypesSize' => array('sizes' => $val));
}
$this->request->data['EquipmentTypesSize']['equipment_type_id'] = $id;
$this->EquipmentTypesSize->set($this->request->data);
$this->EquipmentTypesSize->save($this->request->data['EquipmentTypesSize']);
$this->Session->setFlash('Equipment type has been added successfully ', 'default', 'success');
$this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
}
I want to save this as
equipment_type_id | size1
equipment_type_id | size2
equipment_type_id | size3
There are several issues - the call to create that Kai mentioned is one of them.
First, here's your code, but I've added comments in places where I see obvious issues (I many not have caught all of them)
<?php
if ($this->EquipmentType->save($this->request->data['EquipmentType'], false)) {
$id = $this->EquipmentType->getLastInsertId();
$this->loadModel('EquipmentTypesSize');
$sizesArray = $this->request->data['EquipmentType']['size'];
foreach($sizesArray as $val){
$data[] = array('EquipmentTypesSize' => array('sizes' => $val));
// WHERE IS THIS $data VARIABLE USED? It seems it's never used?
}
$this->request->data['EquipmentTypesSize']['equipment_type_id'] = $id;
$this->EquipmentTypesSize->set($this->request->data); // NO NEED TO CALL SET HERE - YOU ALREADY PASS DATA IN AS A PARAM TO SAVE
// YOU MUST CALL CREATE BEFORE ADDING A NEW RECORD
$this->EquipmentTypesSize->save($this->request->data['EquipmentTypesSize']);
// I DON'T THINK $this->request->data['EquipmentTypesSize'] HOLDS THE VALUE YOU THINK IT DOES AT THIS POINT
$this->Session->setFlash('Equipment type has been added successfully ', 'default', 'success');
$this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
}
Now, it's hard to tell exactly what you're wanting to do, but here's my attempt to write what you intended. If it doesn't work, it should hopefully set you on the right track.
if ($this->EquipmentType->save($this->request->data['EquipmentType'], false)) {
$id = $this->EquipmentType->getLastInsertId();
$this->loadModel('EquipmentTypesSize');
$sizesArray = $this->request->data['EquipmentType']['size'];
foreach($sizesArray as $val){
$this->EquipmentTypesSize->create();
$this->EquipmentTypesSize->save(array(
'sizes' => $val,
'equipment_type_id' => $id,
));
}
$this->Session->setFlash('Equipment type has been added successfully ', 'default', 'success');
$this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
}
Lastly, two things:
Really, you should push as much of that logic into your EquipmentType model as possible. So you might create a saveWithSizes method in your EquipmentType model that holds most of that code.
You should look into Cake's saveAll method (http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array). Ideally, you'd set your form data up so that you could just call saveAll and have Cake handle it all automatically. That may or may not be possible for your situation though.
Related
I need to filter the recording list and I just only need to get the record verb for making my voicemails but the conference source is not filtered
$temp_arr = array();
foreach ($client->account->recordings->getIterator(0, 50, array('CallSid' => 'call sid here' , 'Source' => 'Recordverb')) as $recording){
$recording_uri = "https://api.twilio.com".$recording->uri;
$arr = array(
'rec' => $recording_uri,
);
array_push($temp_arr, $arr);
}
Are you trying to filter recordings based upon Conference or RecordVerb?
Source from the docs:
The type of call that created this recording. Possible values are
RecordVerb, DialVerb, Conference, OutboundAPI, Trunking.
You also have this 'Source' => 'Recordverb' where you could just have a typo with the lowercase 'v'.
I am late to respond but maybe someone else will get benefit from this. You may simply use this code for getting RecoredVerb
$recordings = $twilio->recordings->read([], 200);
foreach ($recordings as $record) {
if($record->source == 'RecordVerb'){
$record_sid= $record->sid;
}else if($record->source == 'DialVerb'){
}else {
}
}
You can do your desired work inside the conditions.
Im trying use "$this->input->post();" of Codeigniter to do not need specify each field in my form. But im getting troubles when try insert into Database.
Look my controller:
public function cadastrar(){
$var = $this->input->post(null, TRUE);
$this->load->model('m_clientes');
$this->m_clientes->inserir($var);
}
My controller is simplified here because i know how to handle database in codeigniter.
The result of this post was:
Array ( [nome] => Raphael [sobrenome] => Schubert [cpf] => 893.528.432-89 [rg] => 4529875231908472 [telefone] => (53) 2980-5792 [celular] => (53) 9 2180-7529 [rua] => Israel de Almeida [numero] => 859 [cep] => 88.312-000 [bairro] => São Vicente [cidade] => ITAJAÍ [estado] => Santa Catarina [email] => rfswdp#gmail.com [tipo] => pf [cnpj] => 34.827.481/2834-78 [inscricaoestadual] => 34120489032814930128 [razaosocial] => Teste [nomefantasia] => Disney [dataaberturaempresa] => 10/21/15 [proprietario] => Marcos Aurelio )
I normaly use this way to insert:
$data = array(
'user_name' => $this->input->post('user_name',TRUE);
'user_phone' => $this->input->post('user_phone',TRUE);
'user_role' => $this->input->post('user_role',TRUE);
);
$this->name_of_model->inserir($data);
And works...
But i was trying to use just $this->input->post(); to get all fields from form. Because my actualy application will have hundreds of fields and i was trying to do not write each line.
So my model actually was:
public function inserir($var){
if($var!=NULL):
print_r($var);
$this->db->insert('tb_usuarios',$var);
endif;
}
But i`m getting and error saying:
Message: Undefined property: Clientes::$db
and
Message: Call to a member function insert() on null
My table name is: "tb_usuarios"
I changed all fields in database to accept NULL to see if i`m get some field name wrong... but not work...
Any tips??
There is no need to catch the POST var inside $var. You can see POST variable inside the model very well. So all you need to do in the controller is:
public function cadastrar(){
$this->load->model('m_clientes');
$this->m_clientes->inserir();
}
,and inside your model:
public function inserir(){
if( count($this->input->post()) > 0):
$this->db->insert('tb_usuarios',$this->input->post());
endif;
}
The fields names in your form must correspond to the column names inside your table.
The message Message: Call to a member function insert() on null means you forgot to load the database library, just like remiheens said.
But my advice is, to use form validation for your fields, so you may be sure all necessary fields are completed using the required data format for each one of them. Although this may require allot of coding, there is no other safe way from errors on database operations. You cannot trust the user to insert the data correctly, that's why you need form validation.
In here $var = $this->input->post(null, TRUE); you use null. null is not valid input name. name = ''
and this will works $this->input->post('user_name',TRUE);
cz of it has input tag name (name = 'user_name').
We use ,TRUE) next to input post field to allows XSS Protection
$var = $this->input->post(null, TRUE); Is Wrong
while you trying this, it shows
Message: Undefined property: Clientes::$db and Message: Call to a member function insert() on null.
will not work
public function cadastrar(){
$var = $this->input->post(null, TRUE);//will never give valid response
$this->load->model('m_clientes');
$this->m_clientes->inserir($var);
}
Works well
public function cadastrar(){
$this->load->model('m_clientes');
$data = array(
'user_name' => $this->input->post('user_name',TRUE);
'user_phone' => $this->input->post('user_phone',TRUE);
'user_role' => $this->input->post('user_role',TRUE);
);
$this->name_of_model->inserir($data);
$this->load->model('m_clientes');
}
This is because your database isn't loaded into codeigniter instance. ($this->db)
Just try to autoload "database" library (config/autoload.php) or load/connect your database in your model with :
$this->load->database();
Don't forget to edit your config/database.php ;)
I handle hundreds of fields everytime but I also validate basically each one. I always do this:
$customer['name'] = $this->input->post('customer_name');
$customer['age'] = $this->input->post('customer_age');
$customer['country'] = $this->input->post('customer_country');
$customer['city'] = $this->input->post('customer_city');
// validations
if(isAgeValid($customer['age']) == FALSE)
{
echo 'Hold on, your age...hmm check it out!';
return;
}
$this->customers_model->add($customer);
The function that handles the insertion only has this:
public function add($data)
{
$data = $this->security->xss_clean($data);
$this->db->insert('customers', $data);
return $this->db->insert_id();
}
Pretty clean and simple. Now, if you don't want to validate the fields or just want to validate some of them and want to insert the others without validation this is what I purpose based on the previous code:
// validations..
if(isAgeValid());
$customer = array();
foreach($_POST as $key => $value)
$customer[$key] = $value;
$this->customers_model->add($customer);
$data = array(
'user_name' => $this->input->post('user_name');
'user_phone' => $this->input->post('user_phone');
'user_role' => $this->input->post('user_role');
);
$this->load->model('name_of_model');
$this->name_of_model->inserir($data);
Model:
public function inserir($data)
{
$this->db->insert('***', $data);
if ($this->db->affected_rows() > 0) {
return true;
}
}
I have a stream/table named profiles. All of its column are stream-fields. I am trying to restrict the result returned by the the function, get_entries() depending on some criteria. Below is my code:
$data = [
'stream' => 'profiles',
'namespace' => 'users',
'where' => 'user_id = 3' // lets say, this is my criteria
];
$row = $this->streams->entries->get_entries($data); // returns empty
The varaible, $row resulted in empty array. Although there is one row in table, profiles where user_id is 3. I have read the documentation of pyrocms and it pretty much says the exact way to use the where clause (just like above).
NOTE: I have also tried writing like
'where' => 'profiles.user_id = 3'`
joy !to avoid table conflict. Still no
But when I write the code like this:
$row = $this->streams->entries->get_entries($query);
$query = [
'stream' => 'profiles',
'namespace' => 'users'
];
// No where clause this time
$row = $this->streams->entries->get_entries($query);
This time $row returns all rows including the row with user id 3.
I am unable to use the where clause in get_entries in a right way. I might have done some mistake. Help me out guyz
NOTE: I am using community edition.
I think this might be due to a bug (well, not a bug, but a feature that doesn't work as intended).
If I'm intentionally issue a wrong query, the sql query output is
SELECT [ ... ] LEFT JOIN `default_profiles` as `profiles` ON `profiles`.`user_id`=`default_profiles`.`created_by` WHERE (user_id` = 1) ORDER BY `default_profiles`.`created` DESC
Here you see that PyroCMS tries to lookup the data for the "created_by" field. And that doesn't work in this case.
If you disable the 'created_by' field, you should get the correct row:
$this->streams->entries->get_entries(
array(
'stream' => 'profiles',
'namespace' => 'users',
'where' => 'user_id = 3',
'disable' => 'created_by'
)
);
It would be great if you could file an issue on the pyrocms github page. If you won't I'll do it in the next few days.
Model
public function get_entries($table, $where) {
$this->db->select('*');
$this->db->from($table);
foreach ($where as $key => $value) {
$this->db->where($key, $value);
}
$this->query = $this->db->get();
foreach ($this->query->result_array() as $row) {
$array1[] = $row;
}
if ($this->query->num_rows() == 0)
return false;
else
return $array1;
}
call this model function as
$row = $this->streams->entries->get_entries('profiles',array('user_id '=>3));
I'm trying to update a row on my profiles table to reset a users profile picture to the default of user.png. I have the following action in my controller:
public function deleteProfilePicture() {
$this->layout = 'ajax';
// First find the profile ID from the user ID
$profileId = $this->Profile->find('first', array(
'condition' => array('User.id' => $this->Auth->user('id')),
'fields' => array('Profile.id'),
'recursive' => -1
));
$this->Profile->id = $profileId['Profile']['id'];
$this->Profile->saveField('picture', 'user.png', false);
}
However, when I request the URL (/profile/deleteProfilePicture) I get no errors but the database row isn't updated. I have made sure the current profile ID is used by using debug($profileId).
What could be going wrong here?
Edit: The return value of saveField():
array(
'Profile' => array(
'id' => '36',
'modified' => '2013-04-05 14:16:57'
)
)
Try
$this->Profile->id = $profileId['Profile']['id'];
$this->Profile->set(array(
'picture' => 'user.png'
));
$this->Post->save();
I see no error in your code. Try seeing what query is getting executed using this
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
based on the result make changes to your query if you find something wrong.
Or try using this
$data['Profile']['id']=$profileId['Profile']['id'];
$data['Profile'['picture']='user.png';
$this->Profile->save($data);
If you set debug to 2 you could see what SQL is being executed, see if your update is actually firing.
Try this
$this->Profile->read(null, $profileId['Profile']['id']);
$this->Profile->saveField('picture', 'user.png', false);
Why are you setting the validation to false? Do you get an error if you omit that?
I'm using CakePHP 1.3.8, and I've installed the CakeDC Search plugin. I have a Tutorial model, which is in a HABTM relationship with a LearningGoal model.
I have a search action & view in the Tutorials controller with which I can successfully search fields in the Tutorial model. I'd also like to filter my tutorial search results using LearningGoal checkboxes on the same form. I've tried adding various parameters to Tutorial's $filterArgs and TutorialsController's $presetVars. I've also tried moving the relevant $filterArgs to the LearningGoal model. I have not yet been able to successfully trigger the entry for learning goals in $filterArgs.
I think I must be missing something obvious. Or maybe the Search plugin doesn't support what I'm trying to do. Does anyone know how to use this plugin to search on associated models?
So here's what I've figured out. You can combine what's below with the Search plugin directions to search on related models.
The $filterArgs piece in the Tutorial model must look like this:
var $filterArgs = array(
array('name' => 'LearningGoal', 'type' => 'subquery', 'method' => 'findByLearningGoals', 'field' => 'Tutorial.id'),
);
Here's the supporting function in the Tutorial model:
function findByLearningGoals($data = array()) {
$ids = explode('|', $data['LearningGoal']);
$ids = join(',', $ids);
$this->LearningGoalsTutorial->Behaviors->attach('Containable', array('autoFields' => false));
$this->LearningGoalsTutorial->Behaviors->attach('Search.Searchable');
$query = $this->LearningGoalsTutorial->getQuery('all',
array(
'conditions' => array('LearningGoalsTutorial.learning_goal_id IN (' . $ids . ')'),
'fields' => array('tutorial_id'),
)
);
return $query;
}
In TutorialsController, $presetVars should look like this:
public $presetVars = array(
array('field' => 'LearningGoal', 'type' => 'checkbox', 'model' => 'Tutorial'),
);
And in my search action in TutorialsController, I did this:
$this->LearningGoal = $this->Tutorial->LearningGoal;
The Prg component seems to need that.
I am using CakePHP version 2.X
Every time I come to do this in a project I always spend hours figuring out how to do it using CakeDC search behavior so I wrote this to try and remind myself with simple language what I need to do. I've also noticed that although Michael is generally correct there is no explanation which makes it more difficult to modify it to one's own project.
When you have a "has and belongs to many" relationship and you are wanting to search the joining table i.e. the table that has the two fields in it that joins the tables on either side of it together in a many-to-many relationship you want to create a subquery with a list of IDs from one of the tables in the relationship. The IDs from the table on the other side of the relationship are going to be checked to see if they are in that record and if they are then the record in the main table is going to be selected.
In this following example
SELECT Handover.id, Handover.title, Handover.description
FROM handovers AS Handover
WHERE Handover.id in
(SELECT ArosHandover.handover_id
FROM aros_handovers AS ArosHandover
WHERE ArosHandover.aro_id IN (3) AND ArosHandover.deleted != '1')
LIMIT 20
all the records from ArosHandover will be selected if they have an aro_id of 3 then the Handover.id is used to decide which Handover records to select.
On to how to do this with the CakeDC search behaviour.
Firstly, place the field into the search form:
echo $this->Form->create('Handover', array('class' => 'form-horizontal'));?>
echo $this->Form->input('aro_id', array('options' => $roles, 'multiple' => true, 'label' => __('For', true), 'div' => false, true));
etc...
notice that I have not placed the form element in the ArosHandover data space; another way of saying this is that when the form request is sent the field aro_id will be placed under the array called Handover.
In the model under the variable $filterArgs:
'aro_id' => array('name' => 'aro_id', 'type' => 'subquery', 'method' => 'findByAros', 'field' => 'Handover.id')
notice that the type is 'subquery' as I mentioned above you need to create a subquery in order to be able to find the appropriate records and by setting the type to subquery you are telling CakeDC to create a subquery snippet of SQL. The method is the function name that are going to write the code under. The field element is the name of the field which is going to appear in this part of the example query above
WHERE Handover.id in
Then you write the function that will return the subquery:
function findByAros($data = array())
{
$ids = ''; //you need to make a comma separated list of the aro_ids that are going to be checked
foreach($data['aro_id'] as $k => $v)
{
$ids .= $v . ', ';
}
if($ids != '')
{
$ids = rtrim($ids, ', ');
}
//you only need to have these two lines in if you have not already attached the behaviours in the ArosHandover model file
$this->ArosHandover->Behaviors->attach('Containable', array('autoFields' => false));
$this->ArosHandover->Behaviors->attach('Search.Searchable');
$query = $this->ArosHandover->getQuery('all',
array(
'conditions' => array('ArosHandover.aro_id IN (' . $ids . ')'),
'fields' => array('handover_id'), //the other field that you need to check against, it's the other side of the many-to-many relationship
'contain' => false //place this in if you just want to have the ArosHandover table data included
)
);
return $query;
}
In the Handovers controller:
public $components = array('Search.Prg', 'Paginator'); //you can also place this into AppController
public $presetVars = true; //using $filterArgs in the model configuration
public $paginate = array(); //declare this so that you can change it
// this is the snippet of the search form processing
public function admin_find()
{
$this->set('title_for_layout','Find handovers');
$this->Prg->commonProcess();
if(isset($this->passedArgs) && !empty($this->passedArgs))
{//the following line passes the conditions into the Paginator component
$this->Paginator->settings = array('conditions' => $this->Handover->parseCriteria($this->passedArgs));
$handovers = $this->Paginator->paginate(); // this gets the data
$this->set('handovers', $handovers); // this passes it to the template
If you want any further explanation as to why I have done something, ask and if I get an email to tell me that you have asked I will give an answer if I am able to.