CakePHP can't save into Postgres database, but with successful return - postgresql

Does anyone encounter this issue before?
In CakePHP, I'm having this.
$this->loadModel('BankBalance');
$data = array("BankBalance"=> array(bla bla bla...);
$this->BankBalance->save($data);
$log = $this->BankBalance->getDataSource()->getLog(false, false); debug($log);
and the debug return me this
array(
'log' => array(
(int) 0 => array(
'query' => 'BEGIN',
'params' => array(),
'affected' => null,
'numRows' => null,
'took' => null
),
(int) 1 => array(
'query' => 'INSERT INTO bla bla bla...)',
'params' => array(),
'affected' => (int) 1,
'numRows' => (int) 1,
'took' => (float) 7
),
(int) 2 => array(
'query' => 'COMMIT',
'params' => array(),
'affected' => (int) 1,
'numRows' => (int) 1,
'took' => (float) 7
)
),
'count' => (int) 3,
'time' => (float) 14)
In actual fact, the row was not inserted despite successful message returned. But if I copied the generated SQL and run it, it does really INSERT into the table.
I've tried to use saveMany as well, it didn't do the INSERTion but still, return me successful message.
There is no error log in CakePHP. updateAll is working fine for the UPDATE. And now I'm looking for the sql INSERT
I can't even do the ->save into a simple table with single field(character varying(50)), no triggers, nothing. Return me success message but no physical data insert into the table.

If you want to insert data, you need to create a new one by put
$this->BankBalance->create();
anywhere before(best at the beginning)
$this->BankBalance->save($data);
and put
if($this->BankBalance->save($data)){
$log = $this->BankBalance->getDataSource()->getLog(false, false);
debug($log);
}
instead of
$this->BankBalance->save($data);
$log = $this->BankBalance->getDataSource()->getLog(false, false); debug($log);
to know if save successfully

save($entity) expects entity, you provide an array:
$this->loadModel('BankBalance');
$data = array("BankBalance"=> array(bla bla bla...);
$entity = $this->BankBalance->newEntity($data);
$result = $this->BankBalance->save($entity);

Related

Get_term with meta_query

I can figure out this query : my purpose is to have a list of taxonomys (category like) with the number posts related to. Something like :
Concerts (4)
Theater (10)
Cinema (12)
...
"Events" posts are store in a CPT 'event' and they all have an "event_start_date" ACF field. If I don't use the meta_query the "get_term" output well all the post by taxonomy. As soon as I try to get all posts from today's date (with the meta_query), nothing appear.
$terms = get_terms( array(
'post_type' => 'events',
'hide_empty' => false,
'taxonomy' => 'events_category',
// 'orderby' => 'name',
// 'order' => 'ASC',
// 'meta_query' => $meta_query_array_from_today,
'meta_query' => array(
array(
'key' => 'event_start_date',
'value' => date( "Ymd"),
'type' => 'date',
'compare' => 'LIKE',
),
)
) );
I've read a lot of answers but i can't make it works, some of answers found do a ne WP-Query but I don't want post, I need a list of taxonomy (using term->count).
If anyone can guide me, I would be grateful.
Thank's !

how to use auto_increment in fuelphp to create table

I write a fileds like this
$fileds = array(
'id' => array('type' => 'integer','AUTO_INCREMENT'=>true),
'user' => array('type' => 'text'),
'time' => array('type' => 'integer')
);
When I run thisDBUtil::create_table($table, $fileds);
it turn wrong,
exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1 near "AUTO_INCREMENT": syntax error' in /home/yangjun/phpenv/public_html/cmanage/fuel/core/classes/database/pdo/connection.php:239
What is wrong ?
ihave rewrite a new test,and add primary key,but doesn't work,i read the document in fuelphp,no idea~
<?php
/**
* #group test
*/
class Test extends TestCase
{
public static function setUPBeforeClass()
{
DBUtil::set_connection('request');
$table = 'test';
$fileds = array(
'_id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),
'_name' => array('type' => 'test'),
'_data' => array('type' => 'test')
);
$primary_keys = array('_id');
DBUtil::create_table($table, $fileds, $primary_keys);
}
public function test_add_keycode_request()
{
$RequestData = array(
'_id' => '',
'_name' => 'hhh',
'_data' => 'ggg');
$RequestData1 = array(
'_id' => '',
'_name' => 'hhhh',
'_data' => 'gggg');
self::add_request($RequestData);
self::add_request($RequestData1);
Cli::write(__METHOD__ . " √ \n", 'green');
}
public static function add_request($RequestData)
{
try {
$result = \DB::insert('test')
->set($RequestData)
->execute('request');
} catch (\Database_Exception $e) {
\Log::error('DB access Error' . $e);
throw $e;
}
return $result;
}
}
error log,this is whole error log,synatax error?
Test
1.exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1 near "AUTO_INCREMENT": syntax error' in /home//phpenv/public_html/fuel/core/classes/database/pdo/connection.php:239
2.Next exception 'Fuel\Core\Database_Exception' with message 'SQLSTATE[HY000]: General error: 1 near "AUTO_INCREMENT": syntax error with query: "CREATE TABLE IF NOT EXISTS "test" (
"_id" int(11) NOT NULL AUTO_INCREMENT,
"_name" test NOT NULL,
"_data" test NOT NULL,
PRIMARY KEY "_id" ("_id"));"' in /home/yangjun/phpenv/public_html/fuel/core/classes/database/pdo/connection.php:272
Seems is missing the constraint param on columns field (i don't know also if int or integer type field).
Try this:
$fileds = array(
'id' => array('constraint' => 11,'type' => 'integer','AUTO_INCREMENT'=>true),
'user' => array('type' => 'text'),
'time' => array('constraint' => 11, 'type' => 'integer'))
}
UPDATE
Need to specify the primary key also:
DBUtil::create_table($table, $fileds, array('id'));
More info in the doc here
Hope this help

First/Last record is not coming with data in cakephp+mongo

I m using the mongodb plugin
ichikaway/cakephp-mongodb
And cakephp 2.6.1
The data in post collection
link to image
And in cakephp it is showing me like
link to image
Cakephp controller side code:
$params = array(
'fields' => array('title', 'body', 'hoge'),
//'fields' => array('Post.title', ),
//'conditions' => array('title' => 'hehe'),
//'conditions' => array('hoge' => array('$gt' => '10', '$lt' => '34')),
//'order' => array('title' => 1, 'body' => 1),
'order' => array('_id' => "DESC"),
'limit' => 35,
'page' => 1,
);
$results = $this->Post->find('all', $params);
I want to pull each and every data from mongodb but this plugin is not providing me the last data.
I have checked the count that's correct.
Am I correct in assuming that this error only appeared after upgrading the PHP driver to 1.6.0? The CakePHP module uses hasNext() and getNext() in MongodbSource::read(), which will be affected by a bug (PHP-1382) introduced in 1.6.0. A fix has already been merged and should be released as 1.6.1 on 2015-02-05 (tomorrow).
For additional context, see a previous answer on the subject: https://stackoverflow.com/a/28304142/162228
In MongodbSource.php file I have made just simple change
in read function
There was a code
public function read(Model $Model, $query = array(), $recursive = null) {
........
while ($return->hasNext()) {
$mongodata = $return->getNext();
Changed it to
public function read(Model $Model, $query = array(), $recursive = null) {
........
foreach ($return as $data) {
$mongodata = $data;
And yurekaaaa,its working fine.

building form in drupal 7 - checkbox value insert into database

Im trying to get checkbox values into SQL database but get this ERROR MESSAGE:
PDOException: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1: INSERT INTO {1test} (personnummer, arende, kampanj) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2_ett, :db_insert_placeholder_2_tva, :db_insert_placeholder_2_tre); Array ( [:db_insert_placeholder_0] => 8007289690 [:db_insert_placeholder_1] => saldo [:db_insert_placeholder_2_ett] => ett [:db_insert_placeholder_2_tva] => 0 [:db_insert_placeholder_2_tre] => 0 ) in my_module_my_form_submit() (line 118 of C:\acquia-drupal-sites\sql\sites\all\modules\1-custom\my_module\my_module.module).
My Form_Module:
$form['kampanj'] = array(
'#default_value' => array(
),
'#required' => '0',
'#key_type_toggled' => '0',
'#weight' => '2',
'#type' => 'checkboxes',
'#options' => array(
'ett' => t('ett'),
'tva' => t('två'),
'tre' => t('tre'),
),
'#title' => t('Kampanj'),
);
function my_module_my_form_submit($form, &$form_state) {
$personnummer = db_insert('1test')
->fields(array(
'kampanj' => $form_state['values']['kampanj']
))
->execute();
drupal_set_message(t('Registrerat.'));
}
If your table has 4 columns, then you need to send it 4 values in the fields function array. You are only sending it 1. Hence the error:
Column count doesn't match value count at row 1
I think in the array you send to the fields function you need to have 4 values with the array key corresponding to the table column. send Null for the id
eg.
->fields(array(
'id' => null,
'id-number' => 'something',
'items' => 'something,
'kampanj' => $form_state['values']['kampanj'],
))
Also, is the value of 'kampanj' actually text as the database is expecting?

Insert email when creating account with rest api

I am faced with a Sugar 6.3 CE installation, trying to create new accounts through rest api.
I am still on the learning curve for a lot of the innards of the CRM, and cannot figure out how to have the email inserted whith the rest of the account info upon creation with a REST call.
I have tried many different field values, and after catching that $email1 was used in some snippet examples I saw on the sugarCRM site. I have not found other mentions in the forums or in the docs yet.
The $parameters array used to configure the usual rest call to create an account in php with REST api looks like this and works fine, except for the $email1:
$parameters = array(
'session' => $session,
'module' => 'Contacts',
'name_value_list' => array(
array('name' => 'first_name', 'value' =>
utf8_encode($contacts["Name"])),
array('name' => 'last_name', 'value' =>
utf8_encode($contacts["GivenName"])),
array('name' => 'phone_work', 'value' =>
utf8_encode($row->PrimaryPhoneAreaCode . ' ' . $row->PrimaryPhone)),
array('name' => 'phone_fax', 'value' =>
utf8_encode($row->PrimaryFaxAreaCode . ' ' . $row->PrimaryFaxNumber)),
array('name' => 'title', 'value' =>
utf8_encode($contacts["Title"])),
/*
* PROBLEM HERE!
*/
array('name' => 'email1', 'value' =>
utf8_encode($row->PrimaryEmail)),
array('name' => 'primary_address_street', 'value' =>
utf8_encode($row->Address1) . ' ' .
utf8_encode($row->Address2)),
array('name' => 'language', 'value' =>
utf8_encode($row->Language)),
array('name' => 'assigned_user_id', 'value' =>
get_rep_id($row->Salesperson1Name, $sugarlink)),
)
);
I would be curious, if someone has the trick. I tried to find to field for emails but it seems to be in separate tables. Any help / tips appreciated.
If you are using REST API the email1 will work for both set en get methods (just tested it).
Did not used the SOAP API as in your example, and suggest to migrate all to REST according to SugarCRM recommendations.
You must add the email into emailadress database first, create contact and set a relationship between both ;-)
$set_entry_parametersEADDR = array(
"session" => $session_id,
//The name of the module from which to retrieve records.
"module_name" => "EmailAddresses",
//Record attributes
"name_value_list" => array(
array('name' => 'email_address', 'value' => $email),
array('name' => 'email_address_caps', 'value' => strtoupper($email)),
array('name' => 'invalid_email' , 'value' => 0),
array('name' => 'opt_out', 'value' => 0),
array('name' => 'date_created' , 'value' => date('Y-m-d H:i:s')),
array('name' => 'date_modified', 'value' => date('Y-m-d H:i:s')),
array('name' => 'deleted' , 'value' => 0),
),
);
$set_entry_resultEmailsAdd = call("set_entry", $set_entry_parametersEADDR, $url);
print_r($set_entry_resultEmailsAdd);
$Email_id = $set_entry_resultEmailsAdd->id;
$set_entry_parameters_contact = array(
//session id
"session" => $session_id,
//The name of the module from which to retrieve records.
"module_name" => "Contacts",
//Record attributes
"name_value_list" => array(
//to update a record, you will nee to pass in a record id as commented below
//array("name" => "id", "value" => "9b170af9-3080-e22b-fbc1-4fea74def88f"),
array("name" => "first_name", "value" => $prof["Prenom"]),
array("name" => "last_name", "value" => $prof["Nom"]),
array("name" => "login_c", "value" => $prof["Login"]),
//array("name" => "email1", "value" => $email),
array("name" => "fonction_c", "value" => "prof")
),
);
$set_entry_result_contact = call("set_entry", $set_entry_parameters_contact, $url);
print_r($set_entry_result_contact);
$contact_id = $set_entry_result_contact->id;
$set_relationship_parameters_email = array(
'session' => $session_id,
'module_name' => 'Contacts',
'module_id' => $contact_id,
'link_field_name' => 'email_addresses',
'related_ids' => $Email_id,
);
$set_relationship_result_email = call("set_relationship", $set_relationship_parameters_email, $url);
print_r($set_relationship_result_email);
Currently I think that not works with REST api but works with SOAP API. Could you try to use email1_set_in_workflow key instead of email1?
It's not a very good solution but perhaps that could unlock you pending a better way to do that in future release