<commit_before><?php
class Field_Reference extends Field {
    public $model;
    public $display_field=null;
    public $dereferenced_field=null;

    function setModel($model,$display_field=null){
        $this->model=$model;
        if($display_field)$this->display_field=$display_field;

        $this->owner->addExpression($this->getDereferenced())
            ->set(array($this,'calculateSubQuery'))->caption($this->caption());

        $this->visible(false);

        return $this;
    }
    function ref($load=true){
        if(is_string($this->model)){
            $this->model=preg_replace('|^(.*/)?(.*)$|','\1Model_\2',$this->model);
            $this->model=$this->add($this->model);
        }

        $model=clone $this->model;

        if(!$load)return $model;
        if(!$this->get())throw $this->exception('Reference field has no value')
            ->addMoreInfo('model',$this->owner)
            ->addMoreInfo('field',$this->short_name)
            ->addMoreInfo('id',$this->owner->id)
            ;
        return $model->load($this->get());
    }
    function getDereferenced(){
        if($this->dereferenced_field)return $this->dereferenced_field;
        $f=preg_replace('/_id$/','',$this->short_name);
        if($f!=$this->short_name)return $f;

        $f=$this->_unique($this->owner->elements,$f);
        return $f;
    }
    function destroy(){
        $this->owner->getElement($this->getDereferenced())->destroy();
        return parent::destroy();
    }
    function calculateSubQuery($model,$select){
        if(is_string($this->model)){<commit_msg>Sigh, when model has hooks, it can't be cloned :(<commit_after><?php
class Field_Reference extends Field {
    public $model;
    public $display_field=null;
    public $dereferenced_field=null;

    function setModel($model,$display_field=null){
        $this->model=$model;
        if($display_field)$this->display_field=$display_field;

        $this->owner->addExpression($this->getDereferenced())
            ->set(array($this,'calculateSubQuery'))->caption($this->caption());

        $this->visible(false);

        return $this;
    }
    function ref($load=true){
        if(is_string($this->model)){
            $model=preg_replace('|^(.*/)?(.*)$|','\1Model_\2',$this->model);
            $model=$this->add($model);
        }else{
            $model = clone $this->model;
        }


        if(!$load)return $model;
        if(!$this->get())throw $this->exception('Reference field has no value')
            ->addMoreInfo('model',$this->owner)
            ->addMoreInfo('field',$this->short_name)
            ->addMoreInfo('id',$this->owner->id)
            ;
        return $model->load($this->get());
    }
    function getDereferenced(){
        if($this->dereferenced_field)return $this->dereferenced_field;
        $f=preg_replace('/_id$/','',$this->short_name);
        if($f!=$this->short_name)return $f;

        $f=$this->_unique($this->owner->elements,$f);
        return $f;
    }
    function destroy(){
        $this->owner->getElement($this->getDereferenced())->destroy();
        return parent::destroy();
    }
    function calculateSubQuery($model,$select){
        if(is_string($this->model)){
{'fraction_non_alphanumeric': 0.1906396255850234, 'fraction_numerical': 0.0012480499219968799, 'mean_word_length': 2.8907766990291264, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7521993', 'n_tokens_mistral': 990, 'n_tokens_neox': 954, 'n_words': 126}