How can I insert an object in database in kohana 3?
My code is:
$application = DB::query(Database::SELECT,"SELECT * FROM application_settings WHERE 'id' = 1")->as_object()->execute();
$application -> google_analytical = $_POST['google_txt'];
$application = DB::insert('application_settings',$application)->execute();
$this->template->inner->status_msg = "Record has been saved successfully";
I want to insert the object named application into the database.
DB::insert() syntax is:
DB::insert('application_settings', $application_columns)
->values($application_values)
->execute();
By the way, why do you want to INSERT new record instead of UPDATEing an existing one?
DB::update('application_settings')
->set(array('google_analytical' => $_POST['google_txt']))
->where('id', '=', $application->id))
->execute();
UPD. You are looking for a single row, so:
// insert it after DB::query() call
$application = current($application);
Related
I have two tables:
NewsRooms ( NrID[int] , NrName [string]);
RawNews( RnID [int], NrID[string]);
realtion is RawNews 1 * NewsRooms
so i use checkboxes for NewsRooms and save the ids as a string in RawNews like this ';1;2;'
now for example i have a list which contains some NrIDs . i want to select every RawNew which it's NrID contains any of the ids inside that list.
here is my code:
var temp = Util.GetAvailibleNewsRooms("ViewRawNews");
List<string> ids = new List<string>();
foreach (var item in temp)
ids.Add(";" + item.NrID.ToString() + ";");
model = db.RawNews.Where(r => r.NrID.Any(ids));
the line model = db.RawNews.Where(r => r.NrID.Any(ids)); is wrong and i don't know how to write this code. please guide me. thanks
ok guys, i found the solution myself so i post it here maybe some other guy need it some day!!
model = model.Where(r => ids.Any(i => r.NrID.Contains(i)));
I have CSV field which is coming from SQL DB. But when shown in gridview i want to replace csv with New Line. I have tried on working out with below code. but still doesnt work on web apps.
var query = (from r in objEntities.Student
select new
{
FullName = r.FullName.Replace(",", System.Environment.NewLine)
}).ToList();
GridView1.DataSource = query;
GridView1.DataBind();
Looking forward!
You need to replace with <br /> : A line break
and also you may need to set HtmlEncode="false" property of the column if it is BoundField
if the column is auto generated to set HtmlEncode as false check below post
Prevent HTML encoding in auto-generated GridView columns
Html does not recognize new lines as actual line breaks when rendered. Instead try this:
var query = (from r in objEntities.Student
select new
{
FullName = r.FullName.Replace(",", "<br />")
}).ToList();
How do I return the correct $insert_id when using a sub query in Kohana?
I'm using the query method to return $insert_id and $affected_rows. It returns the correct value for $affected_rows but returns '1' for $insert_id which is incorrect.
Query below:
$sub = DB::select('id', 'username', 'email', 'lastVisitDate')->from('jos_users');
$qry_migrate_users = DB::insert('temp_users', array('old_id', 'username', 'email_work', 'last_login'))->select($sub);
list($insert_id, $affected_rows) = $qry_migrate_users->execute($this->conn_target);
MySQL returns only last insert id and affected rows. there is only one way to do what you want - execute your sub-select to array, and using foreach do single inserts. But it's a lilte bit slower operation! Or after inserting do something like that:
SELECT id FROM temp_users WHERE email IN (select email from jos_users)
You might understand the logic
How can I fetch selected data to an object and retrieve the selected columns name with PDO?
Now, I'm fetching to a Member object in a loop, and manually add each column to the object by accessing it from the "fetch array". Is there a way to automatically get the column names that's been seleced?
$sql = "SELECT member.name, member.email FROM member WHERE id = :id";
$param = array(':id' => $id);
$stmt = $this->m_db->select($sql, $param);
$ret = new MemberArray();
while ($f = $stmt->fetch()) {
$member = new Member($f['name'], $f['email']);
$ret->add($member);
}
return $ret;
I found the solution in PDOStatement::bindColumn and PDO::FETCH_OBJ
I'm currently working with the versant object database (using jvi), and have a case where I need to query the database based on an object id.
The problem is I'm running some performance tests on the database using the pole position framework, and one of the tests in that framework requires me to fetch an object from the database using either an object reference or a low level object id. Thus, I'm not allowed to reference specific fields in the employee object, but must perform the query on the object in its entirety. So, it's not allowed for me to go "select * from Employee e where e.id = 4", I need it to use the entire object.
What I'm trying to achieve is something along the lines of
Employee employee = new Employee("Mr. Pickles");
session.commit();
FundVQLQuery q = new FundVQLQuery(session,
"select * from Employee employee where employee = $1");
q.bind(employee);
q.execute();
However, this throws an EVJ_NOT_A_VALID_KEY_TYPE error. Does anyone know the correct way of doing this?
Sure you figured this out (post was months ago). What you want to do is use the GetObjectId first, to get the VOD Id of the object, then query the DB;
id = session.GetObjectId(employee);
This is how I did the whole roundtrip object → OID → object:
First you get the OID with TransSession.getOidAsLong.
TransSession session = ...;
Employee employee = new Employee("Mr. Pickles");
long oid = TransSession.getOidAsLong(employee);
session.commit();
Once you have the object ID, just grab the object from its Handle.
TransSession session = ...;
Employee employee = (Employee)session.returnHandleFromLong(oid).handleToObject();
No VQL needed.
Usually keys are integers and not strings. You are creating an Employee using just his name, perhaps the correct identifier to use is his employeeId. I need some more information on the table to know for sure.
You can try this,
FundVQLQuery vql = FundVQLQuery (session,
"select selfoid from Employee where name = $1");
vql.bind ("Mr. Pickles");
HandleEnumeration e = vql.execute ();
while ( e.hasmoreHandles() ) {
Handle handle = e.nexthandle();
}
It will return all Employees with the name "Mr. Pickles", Then loop through them.