Please can someone help with My Model. I want to get users from table users in database (SQL Server) with model builder.
Example:
$cred= new CredModel();
$cred->select('*');
$list=$cred->findAll();
Model returns array(0). I use SQL Server with SQLSRV58 installed on windows.
Thanks for the replies.
you need to declare the table name with from
$cred = new CredModel();
$cred->select('*');
$cred->from('users');
$list = $cred->findAll();
Related
I'm trying to delete table row by table cell ID.
I have a table called "server_admins". With table cell unique ID.
public function admins_delete($id)
{
$serveradmins = DB::table('server_admins')->first();
$serveradmins->delete($id);
return Redirect::to("/admin/servers/admins");
}
Route:
ModuleRoute::post('admin/servers/admins/delete/{id}', 'AdminServersController#admins_delete');
And my view:
But does not work at all... Any info? I'm new with laravel, so im kind a noob on that :) Sorry for dumb ask and thanks for helping me understand laravel.
Try this
Model::where('id',$id)->delete();; // Eloquent approach
DB::table('server_admins')->where('id',$id)->delete(); // Query Builder approach
You can directly chain like this if you know the primary key of the model.
DB::table('server_admins')->destroy($id);
Another method is to call the delete method on after retrieving the model.
$admin = DB::table('server_admins')::find($id);
$admin->delete();
I am new to yii and have a basic doubt.
I am working on an app where I have to store data submitted in one form into a new table.
As I know one table=one model
should I create a new model for the new table using gii?
Can't I just use the following code without creating the model in gii?
$modelA = new table_name;
$modelA->attributes = $_POST['table_name'];
$modelA->save();
No, you have to create model, because:
$modelA = new ModelClassName();
and not table name. You still may use relations for other tables, so you can have only one model:
$modelA->tableNameB->attributes = $_POST['attributes4B'];
I have common model, which is generated by gii.
3 columns in mySql: id (int, A_I), setting(tinytext, null) and value(tinitext, null).
After this:
$cfg = new Config();
$cfg->setting = "sdd";
$cfg->value = 'dsf';
$cfg->save();
This effect I get even if create absolutely clear, new table and model.
This code runs in defaultAction.
Yii 1.1.4
PHP 5.5
MySQL 5.6.12
Help me, I'm tired search this bug =)
You said that the code runs in defaultAction. Is possible that you are calling it also in some other action. This way, the code runs twice.
I have created a migration for ratings, and the table also working when i am entering phpmyadmin.
The problem is, i cannot figure out, how to write to the table?
I am running the code from "story" controller
I am using this:
$z = new Rating();
$z->story_id = 10;
$z->save();
print_r($z);
My "ratings.php" model:
<?php
class Rating extends Eloquent {
protected $table = 'ratings';
}
?>
Is there some place where i should notify laravel that new Rating() means my table "ratings"?
It doesn't seem like i have done the migration correctly, but i am completely new still, so hope someone can figure it out for me.
well instead of using the save() function for laravel you can use the insert() function
Rating->insert_get_id(array('story_id' => '10'));
or
$insert_id = Rating->insert_get_id(array('story_id' => '10'));
for insertion into table.This is much easy to use and I have used this in my whole project and so far I haven't face any problems.
Also if you have not created the model for rating table then go to the models folder under application folder and create a file name rating.php and inside the file write this:
class Rating extends Eloquent
{
public static $timestamps = false;
}
Also please note that table which you created in the phpmyadmin should have name of the form "ratings".
I hope this can be of some help.
I don't really understand what you're doing. Are you trying to write into the table from php? Is Rating a sort of database connection class? You need to create a mysqli object to connect to the database, write a query, and get a result. For best security use a prepared statement. Mysqli Documentation Sorry if I'm off-base about your question, I'm just not positive about what it is.
Using Symfony 1.4 and doctrine I'd like to save a retrieved model to a different database connection:
retrieve model from master-database
change database connection to slave-database
save the model to the slave-database
I have the 2 connections defined in databases.yml.
here in pseudo-code:
$model = [retrieved from master-database];
$slaveConnection = Doctrine_Manager::getInstance()
->getConnection('slave-connection');
$model->save($slaveConnection);
If I create a new model, $model=new model(); the "code" above successfully saves the model to the slave-connection.
What is going wrong?
According to the Symfony log, Symfony recognizes the model as existing and issues an update (instead of an insert).
UPDATE model SET updated_at = '2011-10-21 17:37:32' WHERE id = '1';
Although Symfony is using the correct database connection ('slave-connection'), the update fails because the model isn't present in the slave-database, yet.
And the insert into the slave-database should use all values of the model, not only the changed ones, too.
Anyone can point me to the right direction to save an existing model to a different database?
edit with my solution.
Thanks samura!
Just some additions:
After performing deep copy Symfony saved a new id. But I wanted to really clone the model object to the slave db and so, I had to modify the id.
That caused unique constraint exceptions, so I had to delete first. So this is it:
$id = $model->getId();
$slaveConnection->execute("delete from modeltable where id=".$id);
$model_copy = $model->copy(true); # deep copy
$model_copy->setId($id);
$model_copy->save($slaveConnection);
hope this helps if someone else stumbles.
You could use the public function copy($deep = false) method of the Doctrine_Record class.
$model = [retrieved from master-database];
$slaveConnection = Doctrine_Manager::getInstance()
->getConnection('slave-connection');
$model_copy = $model->copy(true); # deep copy
$model_copy->save($slaveConnection);