laracasts/TestDummy

override relationship when there is more then one.

Closed this issue · 2 comments

HI Ive got a contract Entity that have two relationships the contract model is related to a advertiser as a advertiser can have many contracts , I also need to track which of our sales reps sold the contract so i have a sold_by column that is the id of the user who sold the contract so Its also a relationship now I created a factory which defines both.

$factory('Apps4u\Sharples\Models\Contract',
    [
        'contract_number' => $faker->numberBetween(10000, 99999),
        'sign_date' => $faker->date(),
        'total' => $faker->randomFloat(),
        'deposit' => $faker->randomFloat(),
        'payment_plan' => '50/50',
        'advertiser_id' => 'factory:Apps4u\Sharples\Models\Advertiser',
       'sold_by' => 'factory:Platform\Users\Models\User',

]);

Now when creating the data I override the advertser_id as I want it set the existing record as I have lots of relationships include lots of Polymorphic ones. but when I override the advertiser id but not the userid I get a error saying it can not insert advertiser_id into the users table (there is no link like that). so I can only override a value if I override both relationships.

I create a route to test my seed loading Ive use foreach loops so I can attach polymorphic relationships. As you can see Im override both sold_by (user id) and advertiser_id. But If I only override the advertiser_id I get a error saying that insert into user table failed table dose not have column advertiser_id . Which is correct so I dont know why its trying to do that.

function seed1() {
    Factory::$factoriesPath = base_path() . '/tests/factories';


Factory::times(50)->create('Apps4u\Sharples\Models\Contact');
Factory::times(50)->create('Apps4u\Sharples\Models\Call');
Factory::times(50)->create('Apps4u\Sharples\Models\Site');
Factory::times(50)->create('Apps4u\Sharples\Models\Comment');

$sites = Apps4u\Sharples\Models\Site::all();
$calls = Apps4u\Sharples\Models\Call::all();
$contacts = Apps4u\Sharples\Models\Contact::all();
$comments =  Apps4u\Sharples\Models\Comment::all();

$x = 0;
foreach ($sites as $site) {
    $site->calls()->save($calls[$x]);
    $site->contacts()->save($contacts[$x]);
    $site->comments()->save($comments[$x]);
    $x++;
}

}

function seed2() {
Factory::$factoriesPath = base_path() . '/tests/factories';
Factory::times(50)->create('Apps4u\Sharples\Models\Advertiser');
Factory::times(50)->create('Apps4u\Sharples\Models\Contact');
Factory::times(50)->create('Apps4u\Sharples\Models\Call');
Factory::times(50)->create('Apps4u\Sharples\Models\Comment');

$advertisers =  Apps4u\Sharples\Models\Advertiser::all();
$calls =  Apps4u\Sharples\Models\Call::all();
$contacts =  Apps4u\Sharples\Models\Contact::all();
$comments =  Apps4u\Sharples\Models\Comment::all();

$x = 0;
foreach ($advertisers as $advertiser) {
    $advertiser->calls()->save($calls[$x]);
    $advertiser->contacts()->save($contacts[$x]);
    $advertiser->comments()->save($comments[$x]);
    // create a new contract but not with new advertiser used on of just created advertisers.
    Factory::create('Apps4u\Sharples\Models\Contract', ['advertiser_id' => $advertiser->id, 'sold_by' => 1]);
    $x++;
}
return 'done';

}

get('test2',function(){
/**
 * @return array
 */
function create_sites()
{
    Factory::$factoriesPath = base_path() . '/tests/factories';


    $sites = Factory::times(50)->create('Apps4u\Sharples\Models\Site');

    $x = 0;
    foreach ($sites as $site) {
        $site->calls()->save(Factory::create('Apps4u\Sharples\Models\Call'));
        $site->contacts()->save(Factory::build('Apps4u\Sharples\Models\Contact'));
        $site->comments()->save(Factory::create('Apps4u\Sharples\Models\Comment'));
        $x++;
    }

}

create_sites();


function create_advertisers()
{
    Factory::$factoriesPath = base_path() . '/tests/factories';

    $advertisers = Factory::times(50)->create('Apps4u\Sharples\Models\Advertiser');


    $x = 0;
    foreach ($advertisers as $advertiser) {
        $advertiser->calls()->save(Factory::create('Apps4u\Sharples\Models\Call'));
        $advertiser->contacts()->save(Factory::build('Apps4u\Sharples\Models\Contact'));
        $advertiser->comments()->save(Factory::create('Apps4u\Sharples\Models\Comment'));
        // create a new contract but not with new advertiser used on of just created advertisers.
        Factory::create('Apps4u\Sharples\Models\Contract', ['advertiser_id' => $advertiser->id]);
        $x++;
    }
}

create_advertisers();
return 'done';
});

Ive cleaned up the test seeder and use build for some of the polymorphic relationships . I was able to override the advertiser_id and not the user_id relationship and its looks like its working now . before I used to get 'factory:Platform\Users\Models\Users' set as the user_id for one of my polymorphic relationships which is not the case any more and I can override one relationship and not the other and it now works so thanks for the update and this can be closed