PHPCassa - Can't insert when using uuid timestamp as key - php

I'm trying to store data into cassandra with it being ordered by time. Having trouble using the TimeUUIDType as the key.
I get the following error with phpcassa...
Fatal error: Uncaught exception 'cassandra_InvalidRequestException'
with message 'TimeUUID should be 16 or 0 bytes (6)'
This occurs when calling the insert method...
$pool = new ConnectionPool("Keyspace1", array("localhost:9160"));
$column_family = new ColumnFamily($pool, 'users');
$column_family->insert(CassandraUtil::uuid1(), array('name' => 'value'));
I created a test table using cassandra-cli with the following command...
CREATE COLUMN FAMILY users WITH comparator = TimeUUIDType;

comparator applies to column names, not row keys. If you want the row keys to be TimeUUIDs, you should set key_validation_class instead.
You're getting this exception because Cassandra is expecting a TimeUUID for the column name, but you're passing a normal string.

Related

PHP Phalcon. Use postgres nextval(sequence) in model

I have a table in Postgres with DDL like this one:
CREATE TABLE robots(
robot_id INTEGER NOT NULL CONSTRAINT robot_id_pkey PRIMARY KEY,
name TEXT
);
I know I can insert a record with following SQL statement:
INSERT INTO robots (robot_id, name) VALUES (nextval('robots_seq'), 'WALL-E');
I need to make CRUD operations in Phalcon for this table. Also I want to use ORM features.
So I do:
$robot = new Robots();
$robot->setRobotId(new \Phalcon\Db\RawValue("nextval('robots_seq')"));
$robot->setName('WALL-E');
$robot->save();
And get the following exception:
Uncaught PDOException: SQLSTATE[22P02]: Invalid text representation:
7 ERROR: invalid input syntax for integer: 'nextval('robots_seq')';
Is there any way to accomplish this ?
To tell Phalcon what is name of your model sequence use function getSequenceName:
public function getSequenceName() {
return 'category_id_seq';
}
Phalcon assumes that your models with serial (auto increment) columns will have a sequence named [table_name]_[column_name]_seq.
In your case, in order to make Phalcon take care of handling auto increment columns you should have a sequence named robots_robot_id_seq instead of robots_seq for the column robot_id (which I'd call just "id", by the way).
This way you do not have to worry about setting the id when creating an object and after saving Phalcon will fill that field automatically for you:
CREATE TABLE robots(
robot_id SERIAL PRIMARY KEY NOT NULL,
name TEXT
);
$robot = new Robots();
$robot->setName('WALL-E');
$robot->save();
$robot->getRobotId(); // Should return last "nextval()" sequence
Hope it helps.

How do I get the ID from mongodb with PHP?

I'm trying to get the ID from mongodb to modify and delete user from a simple CRUD app with PHP and vue.js using axios
When I insert any user, I get this id.
{ "$oid": "5a8fd1ef1233610e40007667" }
I just need the ID that mongodb generate itself.
"5a8fd1ef1233610e40007667"
I'm using POST to get the ID and this is what I get when I use var_dump()
string(15) "[object Object]"
¿Any idea of how to get that ID? I've already tried several things, I hope you guys can help me
See the The MongoDB\BSON\ObjectId class.
In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the driver automatically generates an ObjectId for the _id field.
specifically MongoDB\BSON\ObjectId::__toString, which returns the hexidecimal representation of the ObjectId. You can invoke __toString by casting the ObjectId to string, for example:
$stringID = (string)$objectID;
You do not need to create the ObjectID when you insert the record, for example (based on your comment)
$bulk = new MongoDB\Driver\BulkWrite();
$doc = [ 'firstname' => $firstname, 'lastname' => $lastname ];
$bulk->insert($doc);
$result = $manager->executeBulkWrite('db.collection', $bulk);
var_dump($result);
The mongo driver will create and set the '_id' field, insert takes the array by reference and can alter it.

How Can Use Postgresql Array_Append() And Array_Remove() In Yii?

How can use PostgreSQL array_append() and array_remove() in Yii? I am trying to update array type attribute on following table rows-
CREATE TABLE Books(
id INT NOT NULL PRIMARY KEY,
name UUID[])
Sample data rows:
1 NULL
2 NULL
$books=Books::model()->findByPk($id);
$books->name='array_append(name, $Name_key_array)';
$books->save();
I have found following error:
CDbException
CDbCommand failed to execute the SQL statement: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: array value must start with "{" or dimension information
But If I use array directly then its work but not when using variable name i.e.
array_append({1,2,3}, {4})
Also, I already tried few more ways but haven't found any success. I hope will find some great ideas to fix this issue.
I think you are using the array_append() in other way. it should be like this
SELECT array_append(ARRAY[1,2], 3);
array_append
--------------
{1,2,3}
(1 row)
So if i correct yours, it should be
$books->name='array_append($Name_key_array, name)';
for removing elements from an array, check this solution: http://www.youlikeprogramming.com/2013/06/removing-values-from-a-postgresql-array/
to an array in PostgreSQL data must be inserted in the following way,
INSERT INTO table (arr_col) VALUES ('{val1,val2, val3}');
http://www.postgresql.org/docs/9.1/static/arrays.html

Unable to fetch record with least timestamp using PHP

In my mongo database > sessions collection, every record has a fb_id and ts (timestamp). I want to fetch the least timestamp for a particular fb_id. The following query worked successfully in mongoDB shell:
db.sessions.find({fb_id: "88877"},{fb_id: 1,ts: 1}).sort({ts:-1}).limit(1) // works perfectly
I need this functionality in my PHP script, so I wrote following line of code:
echo $tracking->sessions->find(array("fb_id"=> $document["fb_id"]),array("ts"=>1))->sort(array("ts"=>-1))->limit(1)
However, I am getting following error:
PHP Catchable fatal error: Object of class MongoCursor could not be converted to string
What is the correct way to fetch this data?
You need to extract row from the cursor first:
$cursor = $tracking->sessions->find(array("fb_id"=> $document["fb_id"]),array("ts"=>1))->sort(array("ts"=>-1))->limit(1);
print_r($cursor->getNext());

Kohana 2 database insert - insert id is returned but it is protected?

So this code:
$db = Database::instance();
$result = $db->query("insert into parser_log (sent)
values (".sizeof($jobs).")");
returns an object with the insert id, but when I try to access it:
Fatal error: Cannot access protected property Mysql_Result::$insert_id
What is up with that? Must I run a separate query to get the id? seems like a waste since the id is right there.
your code is invalid, you have to pass at least 2 arguments to the query method:
$db->query(Database::INSERT, 'insert into ...');
query method returns an array with last insert id and count of affected rows
You have not specified the line that throws such error

Categories