Note that there are some explanatory texts on larger screens.

plurals
  1. POData not persisted when updating rows with Eloquent
    primarykey
    data
    text
    <p>I'm trying to update a table in an SQL Server database using the Eloquent ORM, but for some reason the update is never committed. As it is a legacy database I have defined my model in the following fashion (as the table and the key do not fully adhere to the expected conventions and the timestamp columns are not named updated_at, created_at)</p> <pre><code>class User extends Eloquent { public static $key = 'UserID'; public static $table = 'User'; public static $timestamps = false; } </code></pre> <p>The code I'm trying to run is as simple as this:</p> <pre><code> $user = User::find($userid); $user-&gt;password = $password; $user-&gt;salt = $salt; $user-&gt;resettoken = $reset_token; $user-&gt;save(); </code></pre> <p>I've debugged the code and can see that the User object is retrieved properly and the attributes of the User object are updated when assigned with new values. However, the save method doesn't persist the data. </p> <p>Using Fluent works like treat, though. This works for example:</p> <pre><code>$affected = DB::table('User') -&gt;where('UserID', '=', $userid) -&gt;update(array( 'password' =&gt; $password, 'salt' =&gt; $salt, 'resettoken' =&gt; $reset_token ) ); </code></pre> <p>But it would be nice to use Eloquent. What's tripping me?</p> <p><strong>UPDATE:</strong> Running the code as Pierre said:</p> <pre><code>&lt;?php $test = new User(); die(var_dump($test)); </code></pre> <p>Returns:</p> <pre><code>object(User)[39] public 'attributes' =&gt; array (size=0) empty public 'original' =&gt; array (size=0) empty public 'relationships' =&gt; array (size=0) empty public 'exists' =&gt; boolean false public 'includes' =&gt; array (size=0) empty </code></pre> <p>The dump of the instance of the User object shows all the attributes, ie this object contains all attributes including the new values, but nothing is saved to database:</p> <pre><code>$user = User::find($userid); $user-&gt;password = $password; $user-&gt;salt = $salt; $user-&gt;resettoken = $reset_token; $user-&gt;save(); </code></pre> <p>Using this syntax makes no difference:</p> <pre><code>$user = User::where_UserID($userid)-&gt;first(); </code></pre> <p><strong>Solution:</strong></p> <p>One has to use a lower case name of the key in the model, then it works.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload