Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
Fixes issue #26: A new record is not saved if it is not dirty.
Browse files Browse the repository at this point in the history
  • Loading branch information
dimsav committed Jun 20, 2014
1 parent 0039b9e commit 82125f7
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
21 changes: 17 additions & 4 deletions Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,31 @@ public function setAttribute($key, $value)

public function save(array $options = array())
{
if (count($this->getDirty()) > 0)
if ($this->exists)
{
if (parent::save($options))
if (count($this->getDirty()) > 0)
{
// If $this->exists and dirty, parent::save() has to return true. If not,
// an error has occurred. Therefore we shouldn't save the translations.
if (parent::save($options))
{
return $this->saveTranslations();
}
return false;
}
else
{
// If $this->exists and not dirty, parent::save() skips saving and returns
// false. So we have to save the translations
return $this->saveTranslations();
}
return false;
}
else
elseif (parent::save($options))
{
// We save the translations only if the instance is saved in the database.
return $this->saveTranslations();
}
return false;
}

public function fill(array $attributes)
Expand Down
16 changes: 16 additions & 0 deletions tests/TestCoreModelExtension.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

use Dimsav\Translatable\Test\Model\Continent;
use Dimsav\Translatable\Test\Model\Country;
use Dimsav\Translatable\Test\Model\CountryGuarded;
use Dimsav\Translatable\Test\Model\CountryStrict;
use Dimsav\Translatable\Test\Model\CountryTranslation;
use Dimsav\Translatable\Test\Model\City;
use Dimsav\Translatable\Test\Model\CityTranslation;
use Dimsav\Translatable\Test\Model\Company;
use Orchestra\Testbench\TestCase;

class TestCoreModelExtension extends TestsBase {
Expand Down Expand Up @@ -142,4 +144,18 @@ public function it_passes_the_N_plus_1_problem()
$this->assertGreaterThan(2, count($countries));
$this->assertEquals(2, $this->queriesCount);
}

/**
* @test
*/
public function it_saves_empty_instances()
{
$company = new Company;
$company->save();
$this->assertGreaterThan(0, $company->id);

$country = new Continent;
$country->save();
$this->assertGreaterThan(0, $country->id);
}
}
22 changes: 22 additions & 0 deletions tests/migrations/2013_11_28_152610_create_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ public function up()
$table->foreign('city_id')->references('id')->on('cities')->onDelete('cascade');
});

Schema::create('companies', function(Blueprint $table)
{
$table->increments('id');
$table->string('name')->nullable();
$table->timestamps();
});

Schema::create('continents', function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
});

Schema::create('continent_translations', function(Blueprint $table)
{
$table->increments('id');
$table->integer('continent_id')->unsigned();
$table->string('name');
$table->string('locale')->index();
$table->timestamps();
});

}


Expand Down
8 changes: 8 additions & 0 deletions tests/models/Company.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php namespace Dimsav\Translatable\Test\Model;

use Illuminate\Database\Eloquent\Model as Eloquent;

/**
* A test class used as a normal class extending Eloquent, not using laravel-translatable.
*/
class Company extends Eloquent {}
15 changes: 15 additions & 0 deletions tests/models/Continent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php namespace Dimsav\Translatable\Test\Model;

use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model as Eloquent;

/**
* A test class that has no required properties.
*/
class Continent extends Eloquent {

use Translatable;

public $translatedAttributes = array('name');

}
7 changes: 7 additions & 0 deletions tests/models/ContinentTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php namespace Dimsav\Translatable\Test\Model;

use Illuminate\Database\Eloquent\Model as Eloquent;

class ContinentTranslation extends Eloquent {

}

0 comments on commit 82125f7

Please sign in to comment.