Giter VIP home page Giter VIP logo

Comments (2)

Swanty avatar Swanty commented on May 26, 2024

P.S. In case someone wants to use JOINs in their SQL query then the previous example might give unexpected issues (at least for me with PostgreSQL) because DB is confused about which table is used for row locking.

Here is an example how to avoid that:

        $table = (new YourAmazingModel())->getTable();
        $table_alias = 'yam';

        $items = DB::table("{$table} AS {$table_alias}")
            ->select("{$table_alias}.*")
            ->leftJoin(' ... left JOIN some other table ... ')
            ->where(' ... filter results by JOINed table ... ')
            ->where(' ... filter results by main (yam) table ... ')
            ->orderBy("{$table_alias}.id", 'asc')
            ->take(15);

        // This is same thing as "lockForUpdate()", but to avoid conflicts with other
        // tables when using JOINs we specify which exact table is used for row locking.
        $sql = "{$items->toSql()} FOR UPDATE OF {$table_alias}";
        $items = $items->getConnection()->select($sql, $items->getBindings(), false);

        // Transform array to YourAmazingModel
        foreach ($items as $key => $item) {
            $items[$key] = (new YourAmazingModel())->newFromBuilder((array)$item);
        }

        $items = collect($items);

        // Now you can loop $items and update them as necessary to release the row locks

from lada-cache.

spiritix avatar spiritix commented on May 26, 2024

Thanks for the explanations. We'll definitely need to implement support for locking mechanisms in Lada Cache. PRs are welcome.

from lada-cache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.