Jump to content

findAndModify() usage in a function


ludo1960

Recommended Posts

Hi guys,

Yet another rookie question, trying to implement this:

I have a function:

<?php //MyHelper.php

 function getnext( $name )
    {

        $conn = DB::table('counter')->where('name', $name )->first();

        $result =  $conn->findAndModify(
            ['name' => $name],
            ['_id' => ['seq' => 1]],
            ['seq' => true],
            ['new' => true, 'upsert' => true]
        );
       return $result['seq'];

    }
?>

And I am calling it so:

                $db_array = getnext(array('name' => 'role_id'));
                print_r( $db_array);

Results in call to a member function on NULL, not sure where I'm going wrong! It is making a connection and the counter table has a 'role_id' in it. Can anyone tell me where I'm going wrong?

Link to comment
Share on other sites

You do realize that you are showing us virtually nothing?  The calls to what seem to be methods of some undefined class cannot be interpreted by us since we have no idea what code we are attempting to access with those calls.  Even the structure of the calls is confusing without knowing the underlying structure of the class' methods.

I assume the class is named 'DB' but I don't know how one creates and uses a line that references somethings called "table", "where" and "first" all in one statement, if that is even what we are supposed to think is happening here.

Link to comment
Share on other sites

Hands up!! So goes the learning curve, maybe I should start at the beginning. I am trying (not very successfully) to create an increment function in the Laravel framework for a mongodb database. _id column . Just to confuse me even more not all of mongoDB functions are available to me as I am using this package. As I understand it mongoDB does not have an native increment field, so my cunning plan was to create a collection and store the incremented values for each $name. So that for example I have a collection named 'counter' and the records are _id = 100 and name = 'some_id'  I want to call a function to increment the value and return the current value of the _id i.e. 101. Sounds simple, but too many things to take into consideration for a rookie. If you help me out, I promise never to laugh at your glorious President ever again, but only if you stop laughing at the joke that is Westminster. Is it a deal?

PS I'm Scottish, so don't care what's going on in London, Angerland as we call it!

Link to comment
Share on other sites

I stand behind my previous post.  Your code makes no sense to us.

For example - what does this SINGLE line do for you?

  $conn = DB::table('counter')->where('name', $name )->first();

It doesn't appear to be calling any function unless you have a class named DB that has a method named 'table'.  And if it does - what exactly are the arguments that you are providing in the call to that method?

Link to comment
Share on other sites

Ok, if you look here that single line should give access to the underlying table data in laravel. Then if you look here and scroll down to the "Incrementing or decrementing a value of a column" it shows how to increment a value in a database field, and the text editor here wont let me switch off bold type, not my day I think. I thought it was clear what I am trying to achieve, a global function to take care of increments.

Edited by ludo1960
Link to comment
Share on other sites

After more digging, i've installed a function helper So to follow your comments I think the classes I need are like this:

<?php

//namespace app;

use Jenssegers\Mongodb\Eloquent\Model;
use Illuminate\Support\Facades\DB;

if (!function_exists('get_next')) {

    /**
     * description
     *
     * @param
     * @return
     */
    function get_next( $name )
    {
        DB::table('counter')

        ->where('name', $name )->increment('_id');

        $retval = array_values(['_id']) ;

        return $retval;

    }
}

The methods I need should be in the classes listed above. Not sure what namespace I need, or if I even need one . Excuse my lack of knowledge, I'm just learning this stuff.

Link to comment
Share on other sites

Your problem is likely with how you're defining your $conn variable.  If you read the code you have and think about it:

$conn = DB::table('counter')->where('name', $name )->first();

That says, roughly 'In the table counter, find rows where name=$name, and give me the first row'

So $conn is going to contain a row of data where as you seem to be expecting it to be a database connection.

Seeing DB::table() makes me assume you are using laravel / eloquent which I'm not familiar with so I can't really provide you the right code.  You'll have to do some looking around to find out how to get the connection or whatever you need for your findAndModify method.

Might just be the table that you need, in which case

$conn = DB::table('counter')

 

Link to comment
Share on other sites

Got it working,

<?php

use Jenssegers\Mongodb\Eloquent\Model;
use Illuminate\Support\Facades\DB;

function get_next( $name )
{
    $old_num = DB::collection('counter')
        ->where( 'name', $name )->pluck('_id');

    $new_num = $old_num[0] + 1;

    DB::collection('counter')
        ->where( 'name', $name )->delete();

    DB::collection('counter')->insert(
        ['_id' => $new_num, 'name' => $name ]
    );

}

Voila, auto incrementing _id field for mongoDB, Thanks for the pointers guys, got a much better understanding of how php works in the laravel framework

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.