how to use auto_increment in fuelphp to create table - fuelphp

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

Related

Unable to get getConfigurationAndPricing in PromoStandard

I am trying to getting Product getConfigurationAndPricing using Promostandard Soap API but for some reason I am unable to get those information. Here is my code snapit
$productId = 'TM17907'
$client = new \SoapClient("https://pspriceconfig100.pcna.online/psPriceConfig.svc?singleWsdl", array('trace' => 1));
$options = array(
'wsVersion' => '1.0.0',
'id' => $apiUsername,
'password' => $apiPassword,
'productId' => $productId,
'currency' => 'USD',
'fobId' => '1',
'priceType' => 'List',
'localizationCountry'=>'US',
'localizationLanguage' => 'en',
'configurationType' => 'Decorated',
'cache_wsdl' => WSDL_CACHE_MEMORY
);
try{
$result = $client->__soapCall('getConfigurationAndPricing',[$options]);
} catch (\Exception $e){
$output->writeln("<error>HTTP issue generate in product ID : ".$productId.", Row No.".$currentRow."</error>");
continue;
}
I am using to get PCNA products.

How to use where/whereDate in mongodb aggregate functions using Jessengers package in Laravel?

How to use where/whereDate in aggregate mongodb query ?
I am using aggregate query in laravel and jessenger-mongodb to group by fields.
The problem here is, I need to add a condition to fetch grouped data of a particular date i.e created_at.
Following code worked perfectly if I do not use pass $date i.e $date = null:
public static function getUsersByTypeLogin($date = null)
{
$q = self::raw()->aggregate(
[
array(
'$match' => array(
'type' => self::LOGIN,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]
);
if(true == $date){
$q = $q->whereDate('created_at','=',$date);
}
return $q;
}
but If I pass $date it returns an error, it says:
message: "Call to undefined method MongoDB\Driver\Cursor::whereDate()"
$date is a Carbon object e.g:
Carbon #1549843200 {#791
date: 2019-02-11 00:00:00.0 +00:00
}
Please help me in this regards.
Try this.
$q = self::raw(function($collection) use($date) {
return $collection->aggregate([
array(
'$match' => array(
'type' => self::LOGIN,
'created_date' => $date,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]);
});

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

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

MongoDB keeps giving me null

I have some difficulty in getting Mongodb aggregate to work. It keeps giving me null. Please help. Below are the codes written in php. Thanks.
What I want to do is to sum up the values of 2 fields, Requests and Responses, between 2 particular dates
try {
$mongodb = new MongoClient("mongodb://ad:pass2word1#localhost");
$database = $mongodb->selectDB('backend');
$collection = new MongoCollection($database, 'RequestSummary');
$pipeline = array(
array(
'$group' => array(
'_id' => array(
'request' => array('$sum' => '$Requests'),
'response' => array('$sum' => '$Responses')
)
)
),
array(
'$match' => array(
'RequestDate' => array(
'$gte' => intval($_SESSION['range_from']),
'$lte' => intval($_SESSION['range_to'])
)
)
)
);
$collection->aggregate($pipeline);
var_dump($g);
} catch (MongoConnectionException $exc) {
echo $exc->getTraceAsString();
}
The _id of your $group can't contain aggregation operators like $sum. Those sums need to be defined as fields at the same level as _id. If you don't want to group on a specific field you can use NULL for the _id like this:
array(
'$group' => array(
'_id' => NULL,
'request' => array('$sum' => '$Requests'),
'response' => array('$sum' => '$Responses')
)
),

FuelPHP Sentry Insert Group on Regstration

I can't really find anything about this in the documentation.
I used Codeigniter a lot, and I used Ben Edmunds Ion auth mostly, and that was a bit easier for me, because there I was able to add a group insert function in the registration.
You can down vote this stupid question, but I can't find anything about this in the Sentry docs.
Here is my register code:
public function action_register()
{
$response = Response::forge();
// validation
$val = Validation::forge('registration');
$val->add_field('first_name', 'Username', 'required|trim|valid_string[alpha,spaces]');
//$val->add_field('first_name', 'Vezetéknév', 'required|');
$val->add_field('last_name', 'Keresztév', 'required');
$val->add_field('email', 'Email', 'required|valid_email');
$val->add_field('email_again', 'Email újra', 'required|valid_email|match_field[email]');
$val->add_field('password', 'Jelszó', 'required|min_length[5]');
$val->add_field('gender', 'Nemed', 'required');
$val->add_field('talent', 'Tehetséged', 'required');
$val->add_field('talent_level', 'Tehetségi szinted', 'required');
if($val->run()):
try
{
// register the user
$user = Sentry::user()->register(array(
'email' => Input::post('email'),
'password' => Input::post('password'),
'metadata' => array(
'first_name' => Input::post('first_name'),
'last_name' => Input::post('last_name'),
'gender' => Input::post('gender'),
'talent_level' => Input::post('talent_level'),
)
));
if($user):
//$email_data = array();
//echo Config::get('base_url'). "user/activate/". $user['hash'];
$email = Email::forge();
$email->from('my#email.me', Config::get('site_name'));
$email->to(Input::post('email'), Input::post('first_name'));
$email->subject('Regisztráció');
$email_data = array(
'name' => "Kedves " . Input::post('first_name'). "<br><br>",
'title' => "Üdvözöllek a ".Config::get('site_name')." oldalán" ."<br>",
'link' => 'Fiókod megerősítéséhez kérlek kattints ide'
);
$email->html_body(\View::forge('email/activation', array('email_data' => $email_data)));
$email->send();
$response->body(json_encode(array(
'status' => 'ok',
)));
else:
$data['errors'] = "error";
endif;
}
catch (SentryUserException $e)
{
$response->body(json_encode(array(
'status' => 'error',
'message' => array(
'email_taken' => $e->getMessage()
)
)));
}
else:
$response->body(json_encode(array(
'status' => 'error',
'message' => array(
'first_name' => $val->error('first_name') ? $val->error('first_name')->get_message() : null,
'last_name' => $val->error('last_name') ? $val->error('last_name')->get_message() : null,
'email' => $val->error('email') ? $val->error('email')->get_message() : null,
'email_again' => $val->error('email_again') ? $val->error('email_again')->get_message() : null,
'password' => $val->error('password') ? $val->error('password')->get_message() : null,
'gender' => $val->error('gender') ? $val->error('gender')->get_message() : null,
'talent' => $val->error('talent') ? $val->error('talent')->get_message() : null,
'talent_level' => $val->error('talent_level') ? $val->error('talent_level')->get_message() : null,
)
)));
endif;
return $response;
}
So my question is how can I make it save the user group selected?
Thank you.
This is taken from the Sentry Documentation; I think this is what you are after:
try
{
// option 1
$user->add_to_group(2);
// option 2
$user->add_to_group('editor');
}
catch (SentryUserException $e)
{
$errors = $e->getMessage(); // catch errors such as user already in group
}