File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,7 @@ public static function ensureModelIsUniqueToQuery($query): void
7676            // This can happen if a certain query, *before having interacted with the library 
7777            // `joinRelationship()` method*, was cloned by previous code. 
7878            $ querysetModel ($ modelnew  ($ querygetModel ()));
79+             $ modelmergeCasts ($ originalModelgetCasts ());
7980
8081            // Link the Spl Object ID of the query to the new model... 
8182            static ::$ modelQueryDictionary$ model$ querySplObjectId
@@ -98,6 +99,7 @@ public static function ensureModelIsUniqueToQuery($query): void
9899
99100            // Ensure the model of the cloned query is unique to the query. 
100101            $ querysetModel ($ modelnew  $ originalModel
102+             $ modelmergeCasts ($ originalModelgetCasts ());
101103
102104            // Update any `beforeQueryCallbacks` to link to the new `$this` as Eloquent Query, 
103105            // otherwise the reference to the current Eloquent query goes wrong. These query 
Original file line number Diff line number Diff line change 1+ <?php 
2+ 
3+ namespace  Kirschbaum \PowerJoins \Tests ;
4+ 
5+ use  Kirschbaum \PowerJoins \Tests \Models \User ;
6+ 
7+ class  WithCastsPreservationTest extends  TestCase
8+ {
9+     /** @test */ 
10+     public  function  test_withcasts_values_preserved_when_joining_relationship ()
11+     {
12+         $ queryquery ()
13+             ->withCasts (['created_at '  => 'date:Y-m ' ]) // Format date as year-month only 
14+             ->joinRelationship ('posts ' );
15+ 
16+         $ model$ querygetModel ();
17+ 
18+         $ this assertArrayHasKey ('created_at ' , $ modelgetCasts ());
19+         $ this assertSame ('date:Y-m ' , $ modelgetCasts ()['created_at ' ]);
20+     }
21+ 
22+     /** @test */ 
23+     public  function  test_withcasts_values_preserved_after_query_is_cloned ()
24+     {
25+         $ queryquery ()
26+             ->joinRelationship ('posts ' )
27+             ->withCasts (['another_field '  => 'date:Y-m ' ]);
28+ 
29+         $ clonedQuery$ queryclone ();
30+         $ clonedQueryjoinRelationship ('images ' );
31+ 
32+         $ model$ clonedQuerygetModel ();
33+         $ this assertArrayHasKey ('another_field ' , $ modelgetCasts ());
34+         $ this assertSame ('date:Y-m ' , $ modelgetCasts ()['another_field ' ]);
35+     }
36+ }
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments