Ok, got this query (works fine, inserts an all...)
$db = Zend_Db_Table::getDefaultAdapter();
$data = array(
"comment_id" => new Zend_Db_Expr("nextval('comments_comment_id_seq')"),
"comment" => $comment,
"comment_level" => 1,
"is_active" => true,
"status" => 0,
"id" => $id,
"user_id" => $_SESSION['role']['user_id'],
"ts_create" => $created,
"ts_mod" => $created
);
$db->insert($this->_name, $data);
$newID = $db->lastInsertId();
I have even tried even
$newID = $db->insert($this->_name, $data);
And I can't get the ID's value. Its not mysql(i) so I think thats my first issue, as postgres doesn't appear to have an autoincriment in the way I am used to working with it atleast. So I am hoping someone here can help me out. Anyway to get the "comment_id" columns id, on a new inserted item? Cause right now I try lastInsertID and all I get is false or 1 which in either case is wrong.
Well GordonM pointed me in the direction of a specific document for zend, that reading through that, I was able to find what I needed. Unfortunately no one provided an actual answer. So I am going to answer my own question, for those who may stumble across it when they are stuck in a similar position as I just was.. But will give the up-vote to GordonM for getting me in the right direction. Thanks GordonM
Changing:
$newID = $db->insert($this->_name, $data);
To:
$newID = $db->lastSequenceId('comments_comment_id_seq');
From the Zend documentation, databases that use sequences to generate autoincrements (like Postgres) require you to specify the sequence by name in the lastInsertId() call.
Auto incremented fields are implemented as a sequence in PostgreSQL, the database reads from this sequence when inserting a new value. You have to use the name of that sequence when calling lastInsertId() to get the correct value.
Related
I am updating an old internal library to use PHP's PDO extension. In the old code, there's a function where the first parameter was a line of SQL and then the second parameter was an optional column to use as the key. I need to keep this same function signature so I don't break any code.
I'd like to use PDO's FETCH_UNIQUE fetch mode, but it would require me to parse every line of SQL that is passed in and add the key column to the beginning of the select. I'm worried that there are edge cases that I wouldn't think of that would break the call.
Is there another way in PDO to designate which column to use as the key when using PDO::FETCH_UNIQUE other than to add it directly to the column list in the SQL call?
Edit:
Here's some example code and more details:
$db = new Database();
$query = "first_name, last_name, contacts_id from contacts where first_name='George'";
$uniqueKeyField = "contacts_id";
$contact = $db->getRows($query, $uniqueKeyField);
Which would yield:
array (
[34] => array (
"first_name" => "George",
"last_name" => "Smith",
),
[452] => array (
"first_name" => "George",
"last_name" => "Johnson",
)
)
The problem is, I cannot always guarantee that the unique keyField will be first in the list like PDO requires because that was not a requirement of our API before.
After going through the manual, my guess is this is not possible to do with PDO::FETCH_UNIQUE and we will either have to use PDO::FETCH_ASSOC and loop through the results like we used to and build our own array or change every call to the above function in existing code to rearrange the fields so the unique keyfield is first. I was hoping someone knew of a way you could just pass the unique keyfield as an argument somewhere, but my guess is PDO faces the same problem we do - they have to have the keyfield in the query somewhere to pull out and put as the id in the returned array and they don't want to have to try to parse and add it to existing queries, thus the requirement to have it as the first field.
Im a bit new to the php side of parse, mainly objective-c and swift but I need to write some code that I can query a column (not the objectID one) to return the results..
The column I'm trying to query is a pointer to another class.
Here is the very basic code I have which returns all the rows in the class and the pointers data with the include key, but I need to filter or get only the row/s that I'm looking for.
$query = new ParseQuery("ClassB");
$query->includeKey("ClassA");
$results = $query->find();
In the php sdk I see an option to use equalTo which has a key and a value to it so I tried the following code.
so I choose the column that was the pointer , and its objectid to hopefully only return those row/s that has that object id.
$query = new ParseQuery("ClassB");
$query->includeKey("ClassA");
$query->equalTo("ColumnNameX", "yjdyaGRWP7");
$results = $query->find();
Nothing was returned and a php error was spit out
'pointer field ColumnNameX needs a pointer value' in /var/www/parse/src/Parse/ParseClient.php:326
So im not 100% sure why I cant filter by a ColumnNameX using its objectID which is a pointer to ClassA..
Did I miss something in the PHP docs..
I mean ideally in mysql to just get that row I want would be
SELECT * FROM ClassB WHERE ColunNameX = yjdyaGRWP7
That would return me the row of data, I can use a Join of course to get some info from ClassA as well.
Any thoughts on what im missing or do I need to first query the Class A to get a pointer, then in the equalTo do something like ("ColumnNamX" , $pointerfromClassA) ?
any one have anyone point out what im missing or have a code example.. I have seen some that use the objectID but I dont have access to that.
Ok I figured out one way to do this, not sure if this is the right way but it returns now what I want..
$query->equalTo("ColunNameX", array("__type" => "Pointer", "className" => "ColunNameX", "objectId" => "yjdyaGRWP7"));
I have searched for this question and found many similar answers like this one
Update a row +1 in CakePHP
and this is what the accepted answer looks like
$this->Widget->updateAll(
array('Widget.numberfield' => 'Widget.numberfield + 1'),
array('Widget.id' => 1)
);
Now iam using this query in cakephp3.
Here is what mine looks like
$Questions=$this->loadModel('Questions');
$Questions->updateAll(
array('questions.trend' => 'questions.trend + 1'),
array('questions.description' => $undashed_title)
);
Every thing is working fine and query's are executing but when i check debugger for sql log, Here's what i found
UPDATE questions SET questions.trend = 'questions.trend + 1' WHERE questions.description = 'What type'
But my value in database is not updating like it should be
(Iam saying this beacuse i also have copied this query on phpmyadmin console and its not working )
I believe the query should look like this
UPDATE questions SET questions.trend = questions.trend+1 WHERE questions.description = 'What Type'
Any help would be appreciated ,Thanks :)
Well, as i said on comments, CakePHP 3 diverge a value from a expression, and the increment thing that you trying to do is a expression, to solve your "problem" you shall dig deep on CakePHP docs, and so you will find this http://book.cakephp.org/3.0/en/orm/saving-data.html#bulk-updates, yeah, right what you want. So, it becomes:
// load your beloved model
$questions = $this->loadModel('Questions');
// create a beauty expression
$expression = new QueryExpression('questions.trend + 1');
// execute a update with the beauty expression
$questions->updateAll(
array('questions.trend' => $expression),
array('questions.description' => $undashed_title)
);
And don't forget to load QueryExpression's namespace with use Cake\Database\Expression\QueryExpression;.
Yes, this is the wrong way to do on CakePHP 3, you shall the CakePHP's ORM, and move the increment thing to the model layer.
I have a problem and i couldn't figure it out alone so I'm here because i really need some help D
I need to know if i can add a field with the same name, like, id, and id but with a diferente value
$usedWord = array('word' => $word);
// search for the word based on the array
$found = $collection->findOne($usedWord);
// If it returns
if (empty($found)) { // Here, it'll insert
$info = array('word'=> $word,
'id' => $id,
'path' => $path,
'start' => $startOfTheWord,
'end' => $endOfTheWord);
} else {
}
$collection->insert($info);
//disc from servidor
$conn->close();
On the Else part, i tried codes to add a new 'id', path and so on, the question is, can i create another 'id', 'patch' and so on OR i should create 'id1', path1 and so on ?
OBs:. i used $push and couldn't make it work =\
Not sure I got your question, but if you are sking about duplicate fields here what mongo docs says:
BSON documents may have more than one field with the same name. Most
MongoDB interfaces, however, represent MongoDB with a structure (e.g.
a hash table) that does not support duplicate field names. If you need
to manipulate documents that have more than one field with the same
name, see the driver documentation for your driver.
Anyway I think you should avoid duplicate fields in your documents it looks like there's something wrong with document structure design.
Hello fellow StackOverflowers,
As modifications on my website were needed in result of massive growth and suggestions from the public, I needed to modify my database, which I have already done to adjust it to the visitor suggestions. Right, very confusing so I'll just get to the point:
Current query:
$arow=mysql_fetch_assoc(mysql_query("SELECT * FROM animelist WHERE id = '".$_REQUEST['id']."'"));
$placeholders = array(' ', ',', ':');
$replacements = array('-', '', '');
$title=str_replace($placeholders, $replacements,$arow['name']);
$title=preg_replace("/[^a-zA-Z0-9\s-]/", "", $title);
$link=$title."-".$arow['id'];
$link="Stream-".$title."-Episode-".$row1['episodes_id']."-".$row1['language']."-".$row1['id'];
Initially the part -Episode- was only needed to be named/written as '-Episode-', however to user suggestions and I completely agree, it needs to be dynamic aswell. Lets say (using the terms for reference only) at first the website only had Episodes and not Movies, but now also has Movies. So we want this part to be dynamic aswell. For this we use database information, I have made a column 'type' INT(1) in the table 'items' I suggest 0 to be -Episode- and if value under type is 1 then I suggest it to be -Movie-.
Now the question is how do I correctly implement it in the query? I understand more queries need to be made similar to the one from $title or $row1. this is what I have so far, but it is not complete yet, because I don't know how to:
$link="Stream-".$title."-.$type.-".$row1['episodes_id']."-".$row1['language']."-".$row1['id'];
$type=$arow['type']
Now there should be a code, which I am not sure of how to write correctly, which makes the condition that if type = 0 then echo Episode, elseif type = 1 then echo Movie.
I greatly appreciate the time you took to read this through and hope you can help me out.
Edit:
Assume $row1, is fetched from table named 'videos' and not from 'animelist', however the table 'videos' each video has an 'id' but also has an column named 'anime_id', this anime_id is equal to the 'id' in 'animelist', in short videos id is the post, and anime_id is the category.
More queries need to be written now to balance the game, please help me out, I am stuck.
Thanks in advance,
Inder
$type = ($arow['type']==0) ? "Episode" : "Movie";
or
$type = $arow['type'] ? "Episode" : "Movie";