I have a model called EmploymentApplication that uses a JSON field to hold all of the submitted form data when someone submits their application. This field is simply called "data". I'm working on the resource controller's 'index' method. I only want to return the id, and the first name and last name (that's inside the 'data' JSON field).
I'm trying to figure it out in Tinker (there is currently only one application in the DB):
$result = \App\Models\EmploymentApplications\EmploymentApplication::all(['id', 'data->fname', 'data->lname'])
returns:
Illuminate\Database\Eloquent\Collection {#888
all: [
App\Models\EmploymentApplications\EmploymentApplication {#881
id: 1,
`data`->'$."fname"': ""Betty"",
`data`->'$."lname"': ""Sue"",
},
],
}
So, It's returning the correct data. The part that has me stumped is how to access that data->fname and data->lname from here.
$result[0]->id works just fine.
$result[0]->(backtick)data(backtick)->'$."lname"'
throws:
PHP Parse error: Syntax error, unexpected '`', expecting T_STRING or T_VARIABLE or '{' or '$' on line 1
What is the correct way to access these properties?
I know I could just use array_map to clean up the result before returning it from the controller, but what is the best practice here?
Thank you.
Use column aliases:
$result = EmploymentApplication::all(['id', 'data->fname as fname', 'data->lname as lname']);
$result[0]->fname
$result[0]->lname
I am not sure if this is what you mean, but if I am using tinker and want to get information from my database, this is what I use.
DB::select("SELECT id, fname, lname FROM EmploymentApplication")
or if you are looking for a specific id.
DB::select("SELECT id, fname, lname FROM EmploymentApplication WHERE id=1")
Related
I used this query to update the status column.
$val="1";
vehicles::where('id' , '=' , $veh_status)->update(['status' => $val]);
But when I submitted the status value doesn't change.
you can trace your query by using ->toSql() method !
try this to find whats happening in back
Not sure what the problem is there because you haven't given much info to work with, but you can check these suggestions:
Check if the column is set to be mass assignable in the model class, that is, it is in the fillable[] array.
make sure the id you pass to the where() function is valid.
Try using another function, save() which will achieve the same results you seek, like this;
// filter the vehicle
$vehicle = vehicles::where('id', '=', $veh_id)->first();
or
$vehicle = vehicles::find($veh_id);
$vehicle->status = 1;
$vehicle->save();
Lastly, I noticed your id variable you pass to the where the () function is called $veh_status "presumably - vehicle status" and not $veh_id, "presumably - vehicle id" so probably check that out.
Ref: Laravel Model Update documentation
Ok this error broke our live app and we have no idea of what it is. After a users logs in with Facebook we get this error: Error: pointer field owner needs a pointer value
Any help is appreciated.
When I had this error, it was because I was passing a string instead of an object of the type that I had a pointer to. Try passing the actual object.
Without seeing any code it sounds like you are trying to access the owner field by passing a string instead of explicitly stating and id.
Incorrect:
[query whereKey:#"post"
equalTo:#"1zEcyElZ80"];
Correct:
[query whereKey:#"post"
equalTo:[PFObject objectWithoutDataWithClassName:#"Post" objectId:#"1zEcyElZ80"]];
The problem is that you are parsing STRING, where as it needs to be parsed as a Pointer. Here's a PHP example:
$userId = "123456";
$query = ParseUser::query();
$query->equalTo("objectId", array("__type" => "Pointer", "className" => "_User", "objectId" => $userId));
$result = $query->find();
print_r($result);
It will spit out all the information of the user. I'm sure you can handle the rest. If you are having an issue with iOS, its similar issue just change the syntax.
I have a login system for my webapp that works well using the Zend auth adapter but the problem is I want the email to be case insensitive when a user logs in. I am using Oracle as the back end DB and normally I would user the LOWER(EMAIL)=LOWER(:email) method. I tried to pass that Oracle function in the setIdentityColumn() but I get the error:
The supplied parameters to Zend_Auth_Adapter_DbTable failed to produce
a valid sql statement, please check table and column names for
validity.
protected function _getAuthAdapter()
{
//$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$db = Zend_Registry::get('db');
$authAdapter = new Zend_Auth_Adapter_DbTable($db);
$authAdapter->setTableName('USER_TABLE')
->setIdentityColumn('LOWER(EMAIL)') //Tried to pass LOWER()
->setCredentialColumn('ENCODED_PW')
->setCredentialColumn('PASSWORD');
return $authAdapter;
}
The error is coming from the function _authenticateCreateSelect() in the Zend_Auth_Adapter_DbTable class. The problem is this part of the script:
$dbSelect->from($this->_tableName, array('*', $credentialExpression))
->where($this->_zendDb->quoteIdentifier($this->_identityColumn, true) . ' = ?', $this->_identity);
The quoteIdentifier() method is like PHP quote() and is turning a query like this:
select * from LOWER(:email)
into this:
select * from "LOWER(:email)"
Anyone see a way around this?
Kind Regards
Nathan
Try something like this:
$authAdapter->setTableName('USER_TABLE')
->setIdentityColumn(new Zend_Db_Expr('LOWER(USERID)'))
->setCredentialColumn('PASSWORD');
The problem is that if you pass 'LOWER(USERID)' as a simple string, Zend will put quotes around it, causing it to create an invalid query. Using Zend_Db_Expr will stop Zend doing this.
In my Yii application, I have a model that represents siteconfig table and have four columns:
integer config_id,
string key,
string value,
string update_time.
I created a model using Gii (to ensure that I will not make any mistakes). I don't publish entire code here, cause this is 100% unmodified by me, standard model code generated by Gii. Since my problem is related to search, I only publish important part of generated code (the search() method):
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('config_id',$this->config_id);
$criteria->compare('key',$this->key,true);
$criteria->compare('value',$this->value,true);
$criteria->compare('update_time',$this->update_time,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
I'm trying to use generated model in normal Yii ActiveRecord search like that:
$etona = new SiteConfigurationRecord();
$crit = new CDbCriteria();
$crit->select = "value";
$crit->condition = "key=:key";
$crit->params = array(":key"=>"sitename");
$etona = $etona->find($crit);
But, instead of getting expected search results, a strange (for me) error occurs:
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]:
Syntax error or access violation: 1064 You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near 'key='sitename' LIMIT 1' at line 1.
The SQL statement executed was: SELECT value FROM siteconfig t
WHERE key=:key LIMIT 1
Where did I go wrong?
You used key for column name, which is a reserved word in MySQL. Yii uses table alias in queries, but does not take any special care in case of reserverd word used as columns names. So, you have to take care of this by yourself.
For example:
$etona = new SiteConfigurationRecord();
$crit = new CDbCriteria();
$crit->select = "value";
$crit->condition = "t.key=:key"; // 't' is default alias
$crit->params = array(":key"=>"sitename");
$etona = $etona->find($crit);
This should solve your problem.
As #Dmitry explained, SQL doesn't allow you to use the column name key. The Yii call in the code in your answer works because Yii performs parameter binding automatically, using names other than reserved words for the parameters. And it also uses fully-qualified column names (prefixes all column name references with <tablename>., regardless of what invalid column name (reserved words) you pass the findByAttributes method.
now it works.. ^^
i just use this code...
$etona = SiteConfigurationRecord::model()->findByAttributes(array('key'=>'sitename'));
maybe i need to study activerecord more somehow...
but still i don't know why the code above doesn't work
I am trying to use a query with a calculated field in a Yii Relationship definition but all I get is errors.
Here is my query: $me = new CDbExpression('CONCAT_WS(\', \', last_name, first_name) AS the_name');
Here is my relation: 'author' => array(self::BELONGS_TO, 'Author', 'auth_id', 'select'=>$me),
My problem seems to be that CDbExpression is expecting a parameter but the query requires no parameters!?!?!?
I'm getting an Error 500 "trim() expects parameter 1 to be string, array given" (because I have no parameter!?!).
If I add a fake parameter: $me = new CDbExpression('CONCAT_WS(\', \', last_name, first_name) AS the_name',array('test'=>'test'));
I get the same error message.
What am I doing wrong?
It sounds like trim() is getting passed an array instead of a string. The "param" referred to isn't the "params" array passed into CDbExpression as the second parameter, but rather the single param passed in to trim().
Could CDbExpression be returning an array instead of a string to the "select" clause in your author relation? I would verify that $me is a string, and not an array.
Also, where is trim() being called? This might shed some light on the subject as well. Turn on all your error reporting to get the file and line number. There are a few places in the DB code where trim() is called.
Try this way:
'author' => array(self::BELONGS_TO, 'Author', 'auth_id', 'select'=>array($me)),