Giter Site home page Giter Site logo

Comments (3)

mdmunir avatar mdmunir commented on June 15, 2024 4

IMO, Much better if we have method update() and delete() at object Query.

// delete
(new Query())
    ->from('order')
    ->where(['status' => 'expire'])
    ->delete();

// update join
(new Query())
    ->from('order o')
    ->leftJoin('invoice i','i.order_id = o.id')
    ->where(['i.status' => 'draft'])
    ->update([
        'o.status' => 'paid',
        'i.status' => 'paid'
    ]);

// delete from relation
$order = Order::findOne($id);
$order->getInvoices()->delete();

from active-record.

stepanselyuk avatar stepanselyuk commented on June 15, 2024

Maybe it will be usefull:

public static function updateAllUsingQuery( $attributes, Query $query, $params = [ ] )
    {

        $params = array_merge( $params, $query->params );

        if (is_null( $query->join )) {
            return static::updateAll( $attributes, $query->where, $params );
        }

        $command = static::getDb()->createCommand();
        $builder = static::getDb()->getQueryBuilder();

        $tableAlias = '{{t}}';

        $sql = $builder->update( static::tableName(), $attributes, $query->where, $params );

        $join = $builder->buildJoin( $query->join, $params );
        $sql = preg_replace( '/^UPDATE (.+) SET/', 'UPDATE $1 ' . $tableAlias . ' ' . $join . ' SET', $sql );

        if ($tableAlias != '') {

            // fixing SET statement
            $sql = preg_replace_callback(
                '/^(.+ SET)(.+?)(WHERE .+)?$/',
                function ( $m ) use ( $tableAlias ) {

                    $m[ 2 ] = preg_replace(
                    // search fields without specified table
                        '/(?<!\.)(?:\[\[|`)(\w+)(?:\]\]|`)(?!\.)/',
                        $tableAlias . '.[[$1]]',
                        $m[ 2 ]
                    );

                    return $m[ 1 ] . $m[ 2 ] . ( isset( $m[ 3 ] ) ? $m[ 3 ] : '' );
                },
                $sql
            );

        }

        $command->setSql( $sql )->bindValues( $params );

        return $command->execute();
    }

In my system the main table usually has alias t, like in CDbCriteria in Yii1.

from active-record.

nirvana-msu avatar nirvana-msu commented on June 15, 2024

+1 would be useful to be able to join tables in updateAll(). Was possible in Yii1.

from active-record.

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.